Getting 400 Message when sending attachment from one ServiceNow Instance to another

mr_zia
Giga Guru

Dear Team,

 

I am using custom application, from the Update Business Rule I am trying to send attachment to another ServiceNow instance but I am getting this error :

{"error":{"message":"Invalid file type: application/json","detail":null},"status":"failure"}

 

Here is my BR code, Experts please help how can i fix this issue 😞

 

var qry = 'table_name=incident^u_sent_flag=false^table_sys_id=' + current.sys_id;
 var att = new GlideRecord("sys_attachment");
    att.addEncodedQuery(qry);
    att.query();
    while (att.next()) {
        try {

            var r = new sn_ws.RESTMessageV2('x_mea_inte_Integration''Send Attachment');
            r.setStringParameterNoEscape('table_sys_id''611b0822fbb392103bf0f471beefdc06'); // sys_id hard coded for testing.
            r.setStringParameterNoEscape('table_name''incident');
            r.setStringParameterNoEscape('file_name', att.file_name);
            r.setStringParameterNoEscape("Accept""application/json");
            r.setStringParameterNoEscape("Content_type", att.content_type);
            r.setRequestBodyFromAttachment(att.sys_id);
            var response = r.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();
            gs.info("Attachment Update Response Body : " + responseBody);
            gs.info("Attachment update status : " + httpStatus);
        } catch (ex) {
            var message = ex.message;
        }
1 ACCEPTED SOLUTION

mr_zia
Giga Guru

I am able to send attachment now.

On the Rest Message Send Attachment Endpoint I have to add : https://instance.service-now.com/api/now/attachment/file?table_name=${table_name}&table_sys_id=${tab...

 

This did the magic!

View solution in original post

5 REPLIES 5

satyaprakas
Tera Expert

Hi @mr_zia ,

 

The error Invalid file type: application/json indicates that the receiving ServiceNow instance is expecting a specific file type, but the request is being sent with a Content-Type of application/json. This is likely because the Content-Type header is being set incorrectly or the receiving endpoint does not accept JSON for file uploads.

 

Update your code as mentioned below

 

var qry = 'table_name=incident^u_sent_flag=false^table_sys_id=' + current.sys_id;
var att = new GlideRecord("sys_attachment");
att.addEncodedQuery(qry);
att.query();
while (att.next()) {
try {
var r = new sn_ws.RESTMessageV2('x_mea_inte_Integration', 'Send Attachment');
r.setStringParameterNoEscape('table_sys_id', '611b0822fbb392103bf0f471beefdc06'); // sys_id hard coded for testing.
r.setStringParameterNoEscape('table_name', 'incident');
r.setStringParameterNoEscape('file_name', att.file_name);

// Set headers correctly
r.setRequestHeader("Accept", "application/json"); // Accept header for response
r.setRequestHeader("Content-Type", att.content_type); // Content-Type for the attachment

// Attach the file content to the request body
r.setRequestBodyFromAttachment(att.sys_id);

// Execute the request
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.info("Attachment Update Response Body : " + responseBody);
gs.info("Attachment update status : " + httpStatus);
} catch (ex) {
var message = ex.message;
gs.error("Error sending attachment: " + message);
}
}

 

 

Please consider marking my reply as Helpful and/or Accept Solution, if applicable. 

Regards,

Satya

Hi Satya,

 

Still, I am getting 400 error

 

Attachment Update Response Body : {"error":{"message":"Invalid file type: image/png","detail":null},"status":"failure"}

Anand Kumar P
Giga Patron
Giga Patron

Hi @mr_zia ,

 

Check the glide.attachment.extensions system property on the target instance the type of file extension your are sending is added.

 

Try using multi part as content type

r.setRequestHeader("Content-Type", "multipart/form-data");.

https://www.servicenow.com/community/developer-forum/any-examples-of-a-multipart-form-data-posting-a...

https://www.servicenow.com/docs/bundle/yokohama-api-reference/page/integrate/inbound-rest/concept/c_...

 

If my response helped, please mark it as the accepted solution and give a thumbs up👍.
Thanks,
Anand

Hi Anand,

That's a very good point because every time I get invalid file type.

 

I dont see glide.attachment.extensions system property on Target instance, but on my instance we have this property,

On the Target Instance there 3609 records in system property out of then 99 are secured that I am not able to view, I will ask the target instance developer tomorrow.

 

Is there a chance glide.attachment.extensions record not there on target instance that could cause the error invalid file type?