How to call a 3rd party REST API with file attached for Content-Type multiple/form-data request

Hank4
Tera Contributor

I'm having trouble to call a 3rd party REST API. I have to call that API with the request
1. Header: Content-Type = multipart/form-data; boundary=xxxx
2. A file must be attached as a part of the request body

I tested the sample call in Postman, and it works well.

find_real_file.png

When working in SN, I'm using the Outbound Integrations > REST Message. I configured my POST request content as below:

find_real_file.png
And the code to call the 3rd party REST API is as below.

find_real_file.png

I can call such API with response code 200 but the 3rd party who owns the REST API told me my request is not able to be processed as 'Unexpected end of Stream, the content may have already been read by another component.' error.

I guess the reason for that error is the content of 'f' I sent out is a string but NOT the binary stream the API expects for.

Now, I'm stuck here for a long time. Would you please provide some help to resolve this?

Is it possible to call the REST API I mentioned above with RESTMessageV2?

If not, then what should I do to make my call to the API?

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I had used multi-part form data with boundary in outbound REST and it worked fine.

so are you sending xml string in that json request or just file name?

Did they share sample request body?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks for the quick response. Ankur.

Yes, using multi-part form data to call REST API via RESTMessageV2 is okay if you don't need to attach a file to the request.

My pain point here is I have to call that API with an XML file attached. I tried calling that API in JSON format and that JSON contains the XML string as the value of parameter 'f'. But they told me it was not able to be processed.

They only shared the request sample I attached as the Postman screenshot. You can see the type of the key 'f' is a file.

find_real_file.png

not very sure then

did you try to print what request body is being sent out?

gs.info(request.getRequestBody());

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader