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

RafaGiRe
Kilo Explorer

Hi guys,

 

I'm trying to build an integration with Google Drive and I can say that the function setRequestBodyFromAttachment() works, because I'm able to send my attachment from ServiceNow to GoogleDrive. 

 

However, it's true that when I try to use the getRequestBody() function after the first one, I get a null result. I don't understand why this function returns null, but the content of the attachment is really inside the body of the REST message, because my attachment appears in Google Drive.

 

I wanted to use that second function because I need to get the attachment in the format that the function setRequestBodyFromAttachment() internally use, but I don't know how to get it. Please, if you know how to do this, let me know.

 

Have a nice day.