Hi All,
when i upload a image as "multipart/form-data" directly to the backend i'm getting success response. But i have a requirement to upload the image as binary and then build a "multipart/form-data" request with the binary content, i tried building the request in JavaScript and send it to the backend but i'm getting an error response.
Please find the below code:
function getMultipartPayload(content, boundary){ var formPayload = ""; formPayload = formPayload + boundary+"\r\n"+"Content-Disposition: form-data; name=\"file\"; filename=\"Sideview.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"+content+"\r\n"; formPayload+= boundary+"--\r\n"; return formPayload; } var content = context.getVariable("request.content"); var boundary = "--------------------------703350424384203211386545"; var multipartPayload = getMultipartPayload(content, boundary); context.setVariable("boundary",boundary); context.setVariable("multipartPayload",multipartPayload);
I used the above set variables in an Assign Message policy to form the request.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <AssignMessage async="false" continueOnError="false" enabled="true" name="AM.Build.Request"> <DisplayName>AM.Build.Request</DisplayName> <Properties/> <Set> <Headers> <Header name="Content-Type">multipart/form-data; boundary={boundary}</Header> </Headers> <Payload variablePrefix="@" variableSuffix="#">@multipartPayload#</Payload> <Verb>POST</Verb> </Set> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> <AssignTo createNew="false" transport="http" type="request"/> </AssignMessage>
and i observed that Content-Length is different from what i received in the request header and when printed in JavaScript. Does Apigee alter the content when we do context.getVariable("request.content") in a multipart/form-data or binary, file upload request?
I saw these related discussions:
https://community.apigee.com/questions/13135/how-do-i-send-multipart-data-to-my-request.html
https://community.apigee.com/questions/34311/how-do-i-calculate-the-content-length-for-a-multip.html
Any suggestions what can be done here?
Solved! Go to Solution.
I don't think you can do what you want in a JS callout. The JS callout does not have good facilities for handling byte streams (aka "binary content"). If the multipart form contains only text, it's easy. But if you want non-UTF-8 byte streams, then JS won't satisfy.
You could do what you want in
I don't think you can do what you want in a JS callout. The JS callout does not have good facilities for handling byte streams (aka "binary content"). If the multipart form contains only text, it's easy. But if you want non-UTF-8 byte streams, then JS won't satisfy.
You could do what you want in
Thanks @Dino-at-Google for the quick response. Yes, i figured out, i was losing some data in JS, so i used a Java callout and it worked.
https://community.apigee.com/articles/60050/multipart-multipartform-data-creation-and-parsing.html
This article is really helpful.
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 |