JavaScript - How to send PDF file to POST /now/attachment/upload
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-18-2022 09:28 AM
I'm trying to attach a PDF file to our tickets via the /now/attachment/upload API.
Having issues figuring out the correct syntax to do this with JavaScript, I have tried the below solution with axios
This returns a 400 - Bad Request error. I'm looking for a code example of using this API with JavaScript
const config = { headers: { "Content-Type": "multipart/form-data" } };
await this._axios.post(
`/now/attachment/upload`,
{
table_name: tableName,
table_sys_id: sysId,
uploadFile: './FilePath/test.pdf',
},
config
);
Labels:
- Labels:
-
Scripting and Coding
5 REPLIES 5
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā10-07-2023 04:34 AM
It seems to only affect PDFs. Both of these work for any other file type, and works fine from postman, the REST API explorer, and normal upload methods but not the $http method. Again other file types work fine:
var sys_id = .....
var url = '/api/now/attachment/file?table_name=.....&table_sys_id=' + sys_id + '&file_name=File'
return $http.post(url, false, {
transformRequest: angular.identity,
headers: {
'Authorization': 'BASIC ' + btoa(MY_CONFIG.username + ':' + MY_CONFIG.password),
'Content-Type': file.type,
'Accept': "application/json, text/plain, */*",
data: {base64pdf: file}
}
})
.then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
console.log(response)
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(response)
});
var FormData = new FormData();
FormData .append('table_name', '....');
FormData .append('table_sys_id', sys_id);
FormData .append('uploadFile', file );
var url = '/api/now/attachment/upload'
request = {
method: 'POST',
url: url,
data: FormData ,
headers: {
'Content-Type': undefined,
'Accept':'application/json',
'Authorization': 'BASIC ' + btoa(MY_CONFIG.username + ':' + MY_CONFIG.password)
}
};
return $http(request).success(function (response) {
// On success code here
console.log(response)
}).error(function (err) {
// On error code here
console.log(err)
});