Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

SaveResponseBodyAsAttachment() method failing with Unable to save response body as attachment: User not authorized .

rahulyamgar
Tera Guru

Hello Friends,

 

I am using the REST API call and saving the response body using the method - "saveResponseBodyAsAttachment(tablename, recordSysId, filename); ". I am setting the MID server to execute the request. Above method execution is failing with "Unable to save response body as attachment: User not authorized " error.

 

Please guide.


Thanks,
Rahul

1 ACCEPTED SOLUTION

This is what we are doing:

1. Getting the ResponseBody.

var response = sm.execute();
var responseBody = response.getBody();

2. Creating an attachment using the below code, where gr is the GlideRecord to which file is attached. 

attachment.write(gr,"SampleJson","json",responseBody); 

 

It worked for me.

 

Thanks,
Rahul

View solution in original post

13 REPLIES 13

Hi, the solution does not work if the content-type of the response is application/pdf or application/octet-stream and in the documentation they do not recommend using getBody for large files.

I also try to return an application/json with the file in base64 and then use attachment.writeBase64. This works but the payload is almost twice the size.

Marc

I ran into the same issue and I had to use saveResponseBodyAsAttachment as I was dealing with binary data; the problem is likely that the account the MID server is running under does not have sufficient rights to create in the target table (After trying custom ACLs on sys_attachment and sys_attachment_doc I found they were not the problem!)

In my case I was retrieving a file to use in a sys_data_source so the fix was simple, I just gave the MID server account the import_admin role which allowed it to write to the Data Source, likely you just need to give the MID server user the relevant permissions for your table_name that you are attaching to.

Hope this helps.

Hi, this solution does not work if the content-type of the response is application/pdf or application/octet-stream.

Hi, this solution does not work if the content-type of the response is application/pdf or application/octet-stream.