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?
