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

aswinsiddaling2
Kilo Guru

Can you remove the 'Content-Type' header and try?


Sorry we get still error as



{"code":"UNSUPPORTED_MEDIA_TYPE","message":"Content type was not provided or is not supported"}


What is the content-type of the attachment that you are trying to send? You can check the content type of each attachment in sys_attachment table.



Specify that content type in the   'Content-Type" header and try. And I am not sure what the "mime-type" header is for, may be remove it and try as well


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 .