How to set Form-data in Rest Message

mohith2
Mega Contributor

I searched through the community and could not find sample code to send file attachment through REST Outbound from servicenow .

I tried the below code so far with no luck

var r = new sn_ws.RESTMessageV2();

  r.setHttpMethod('post');

  r.setEndpoint("endpoint");

  r.setRequestHeader("Accept","application/json");

  r.setRequestHeader('Access-Token',accesstoken);

    r.setRequestHeader('Content-Type','multipart/form-data');

      r.setRequestHeader('Mime-Type','application/pdf');

    r.setRequestBodyFromAttachment('f8cfa45ddbe5f200ddfc7e7dbf9619f6');//attachment Id

  var response = r.execute();

  var responseBody = response.getBody();

  gs.debug(responseBody);

  var httpStatus = response.getStatusCode();

The error I get is as follows

{"code":"INVALID_MULTIPART","message":"An invalid multipart was specified"}

1 ACCEPTED SOLUTION

mohith2
Mega Contributor

Its been confirmed by ServiceNow that they do not support multipart form data .The only solution is either use Mid server or create own proxy server where you can send as Binary Stream .


View solution in original post

14 REPLIES 14

See my response which includes how to do so. (3rd reply to the question)

Hi Brian,

THank you for the quick response,

I have tried but no luck.

find_real_file.png

I couldn't able to convert into raw format.

would you please help me to get the raw converted format?

You have to manually do that. As stated:

Convert your key: value pair to a raw data format of key=value with an & inbetween value and the next key:

 

key=value&nextkey=nextvalue&thirdkey=thirdvalue

Thank you, Brian.