How I can upload a file in POST REST API with argument?

jacc_99
Giga Guru

Hello,

I've uploaded a file throughout Postman POST REST API with this body form-data.

find_real_file.png

user, pass and location_file_path are Text type and loc_rep is File type

When I send this POST REST API with postman the file myCsvTypeFile.csv is saved in /wz01/reports/ and it behaves as I expect.

Now I want to do the same with script.

I made a POST REST Message Method with this HTTP Query Parametersfind_real_file.png

And I have this code calling this method:

var callR = new sn_ws.RESTMessageV2('rest_message_name', 'method_name');
callR.setStringParameter('user_log','user_log_value');
callR.setStringParameter('password','pass_value');
callR.setStringParameter('encodedFile', value_of_file);
callR.setRequestBodyFromAttachment(file_sys_id);
var responsecallR = callR.execute();
responsecallR.waitForResponse(600);
responseBodycallR = responsecallR.getBody();
responseCodecallR = responsecallR.getStatusCode();

 

The result is 200. Success, but the file in this case is not saved in /wz01/reports/.

I receive 200 success status, but empty bodyresponse.

I test this with value_of_file=sys_id attachment I can send to other place in /wz01/reports/.

I test too encoding the attachment file I can send like this

var StringUtil = new GlideStringUtil();
var gsa = new GlideSysAttachmentInputStream(file_sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos, 0, 0);
baos.close();
encodedFile = GlideStringUtil.base64Encode(baos.toByteArray());

The result is the same that the others. I achieve conect with the other system but it return my empty body and therefore the file is not saved in /wz01/reports/

 

Somebody coul help me with that problem?

I need replicate the postman POST REST API with script.

 

Thanks in advance

3 REPLIES 3

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi jacc_99

Sorry but not understanding the question. Is the question about sending a POST request from ServiceNow to some other external system and the other system not being able to correctly process the posted data?

If so, the problem is with the external system processing the data and should be contacting the developer at the other system.

The other side is a system of storage.

Support of that system gave me the parameters to put the file in /wz01/reports/.

Hi,

Check Lab 4 starting at page 14 in the following link for an example to using saveResponseBodyAsAttachment().

file:///C:/Users/works/Downloads/HackLab%20-%20Attachment%20integration%20APIs.pdf