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.

setRequestBodyFromAttachment results in response "post request body is missing content"

Pothuraju
Mega Expert

Hi community,

I am using rest message to post attachments to 3rd party tool, Im using setRequestbodyfromattachment(attacment_sysId);

but ECC queue is returning  "post request body is missing content" error.If I use base64 code as a body it is working fine but not setRequestbodyfromattachment. I have given admin access to mid server.

script is like

var myInput = 'filename.txt';

var myProfile = new GlideRecord('sys_auth_profile'); myProfile.get('name',

gs.getProperty('system.property.authProfile'));

var myAttachment = new GlideRecord('sys_attachment');

myAttachment.get('file_name',myInput);

var myEndPoint = gs.getProperty('system.property.endpoint');

var requestBody; var responseBody; var status; var sm;

try{

sm = new sn_ws.RESTMessageV2("RESTmsg", "RESTmethod");

sm.setAuthenticationProfile('basic', myProfile.sys_id);

sm.setEndpoint(myEndPoint);

sm.setRequestHeader('Accept', 'application/json');

sm.setRequestHeader('Content-Type', myAttachment.content_type); sm.setRequestBodyFromAttachment(myAttachment.sys_id);

sm.setHttpTimeout(10000);

response = sm.execute();

responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();

status = response.getStatusCode(); }

catch(ex) {

responseBody = ex.getMessage();

status = '500'; }

finally {

requestBody = sm ? sm.getRequestBody():null; }

if(status == '200' || status == '201')

{ gs.log("callExternal.sendPOST: Status Code: " + status);

gs.log("callExternal.sendPOST: Request Body: " + requestBody);

gs.log("callExternal.sendPOST: Response: " + responseBody); output = 'PASS'; }

else { gs.log("callExternal.sendPOST: Status Code: " + status);

gs.log("callExternal.sendPOST: Request Body: " + requestBody);

gs.log("callExternal.sendPOST: Response: " + responseBody); output = 'FAIL'; } }

else { output = 'FAIL'; }

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Raju,

Can you try to give contenttype in the rest message as either application/json etc and check once because the content type you are setting is from sys_attachment table which is not the expected value in that http header

sm.setRequestHeader('Content-Type', 'application/json'); 

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader