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

brian_
Tera Guru

I didn't like the answer, so I found out an alternate method: putting your form data into a raw text format.



find_real_file.png


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


find_real_file.png



Then place this into the Content portion of your message


ALSO set a HTTP Header of:



Content-Type: application/x-www-form-urlencoded


find_real_file.png


Thanks Brian. Your approach helped. I needed to pass values to BMC (Outbound REST message) as a Content-Type as application/x-www-form-urlencoded and was able to pass it successfully following your approach.

Thank you very much! 

This was helpful, but also discovered that you will likely need to run any variables through a URI Encoder process before submitting. 

It just so happened one of our test values had a "&" character in it, and this was causing a problem when populating the content block when sending the POST.  It would treat the character as a break to the next key/value pair.

ralf_schnell
ServiceNow Employee
ServiceNow Employee

Hi,

I love the creative thinking using the 'Content' field - great solution! The official ServiceNow solution for this, I guess, is using Flow Designer. The REST step allows you to simply select "form encoded" and then provide the name-value pairs below. I am using this method successfully in automating webinar operations on the ON24 event platform from my ServiceNow instance.

find_real_file.png

Kind regards

Ralf Schnell
Senior Platform Evangelist, ServiceNow

Hi Ralf,

Glad ServiceNow came with excellent solution.

But is it poosible to send From_encoded Request type in rest message without using integrationHub.

Thanks,

Vivek|LinkedIn