setRequestBodyFromAttachment is not working (RESTMessageV2)

SIMTEC
Kilo Contributor

Hi folks!

I am wondering, that there is no solution, after so many years.. If i use the given example of setRequestBodyFromAttachment, i get an empty Request-Body. Tested in background-scripts, script-includes and Xplore. Here is the example Code.. lets take a non-existing endpoint, it should generate a Request-Body anyway:

var attSysid = '35821f291b2bc410fd08fc49cd4bcb48';
			
var r = new sn_ws.RESTMessageV2();
r.setHttpMethod('post');
r.setEndpoint('https://iamanendpoint');
r.setRequestBodyFromAttachment(attSysid);

var reqHeaders = JSON.stringify(r.getRequestHeaders());
var reqBody = r.getRequestBody();
			
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.log('Headers: ' + reqHeaders);
gs.log('Body: ' + reqBody);
gs.log('Status: ' + httpStatus);
gs.log('Response: ' + JSON.stringify(responseBody));

The Output is:

2020-03-14T18:08:31.420Z: Headers: {}
2020-03-14T18:08:31.420Z: Body: null
2020-03-14T18:08:31.420Z: Status: 0
2020-03-14T18:08:31.420Z: Response: ""

The Request-Body is null. Of course, this does not change for a working endpoint: I get back status 200 and an empty response (from a Jira-Instance). But the attachment does not go out, for that the Request-Body is empty!

Also not with the Headers, that the 3rd party expects (multipart/form-data..)!

I guess, this is not the normal behavior? I have seen threads with the same issue, like this one: https://community.servicenow.com/community?id=community_question&sys_id=cd337739dbf32fc054250b55ca96...

Or here: https://community.servicenow.com/community?id=community_question&sys_id=763bb581db551b40fac7f4621f96...

Does this work for anyone? Did i make anything wrong?

Thanks in advance.

5 REPLIES 5

Tanaji Patil
Tera Guru

Though I never got setRequestBodyFromAttachment() working for me but I just want inform you that getRequestBody() will always be null if you are not instantiating a RESTMessageV2 object using REST message record.

So it will return something when use like this-
var r = new sn_ws.RESTMessageV2("REST Message", "HTTP method");

It always returned me the value of the Content field of REST HTTP method used.

 

-Tanaji

Please mark reply correct/helpful if applicable

 

 

Thanks Tanaji,

i appreciate every hint.. Actually i was using your mentioned version of instantiating RESTMessageV2. Just tried the code above, because i wanted to know, if the EXAMPLE CODE also not works!

I have a record for that and call it just like you showed. And this does work fine, but i get "null" also in that case!

I really think, this is a bug, isn't it?

Did you had anything in Content field on the HTTP method you created? If yes, then you get exact that value and not the request body you are setting up in the code.

 

I personally thinks the documentation is lacking with sn_ws.RESTMessageV2() function. But it could also be a bug.

 

-Tanaji

Please mark the reply correct/helpful if applicable

That‘s interresting! No i don‘t have anything in Content field. That is the reason for getting Null?! Even if i first call setRequestBodyFromAttachment() and then getRequestBody()?! Any possibility to control the Request that went out? Thanks, and BTW: have a nice sunday 😉