Need to send a request to a service with contentType="multipart/form-data"

Not applicable

Hi, I am having some troubles on making a request on apigee with the contentType

"multipart/form-data"

this is an example (on postman) of the data I want to send on my request:

2844-form-postman.png

when I try to send that request from postman directly to my API proxy I receive this:

Content-Type multipart/form-data; boundary=----WebKitFormBoundaryqRAOAKBFnLGNQtg0

Request Content

Body

------WebKitFormBoundaryqRAOAKBFnLGNQtg0

Content-Disposition: form-data; name="api_key"
YIfshYF35kRT09GFDM2m3b56

------WebKitFormBoundaryqRAOAKBFnLGNQtg0

Content-Disposition: form-data; name="hash"
1k675a1234bd7u76c62f622ou2997f958

------WebKitFormBoundaryqRAOAKBFnLGNQtg0

Content-Disposition: form-data; name="document_type"
RUT

------WebKitFormBoundaryqRAOAKBFnLGNQtg0

Content-Disposition: form-data; name="document_number"
12345678-9

------WebKitFormBoundaryqRAOAKBFnLGNQtg0

C.....

I tried to do the request by preparing a payload on Assign Message for executing next on a service callout.

I also tried to prepare the same data I showed up here on request content body on a javascript, but none of these has worked for me.

I found a similar question here on community but no solution, this is the similar question

thanks

Carmj.-

Solved Solved
1 6 19.3K
1 ACCEPTED SOLUTION

Sure , Thank you 😄

View solution in original post

6 REPLIES 6

Hi - I don't understand the problem .

"I tried to do the request by preparing a payload on Assign Message".

I'm clear that you want to send a multipart form request out of Apigee Edge. You said "none of these has worked for me."

Can you be more specific about what you are seeing, and what you are expecting to see?

Not applicable

Hi Claudio !

I have the same issue.

I can resolved it with this solution :

1 - I used a javascript for create multipart :

var multipart ="";
var boundary = Math.random().toString().substr(2);
var multipart ="";
    
multipart += "--" + boundary+"\r\n"
           + "Content-Disposition: form-data; name=\"name1\""
           + "\r\n\r\n" + name1 + "\r\n"
           + "--" + boundary+"\r\n"
           + "Content-Disposition: form-data; name=\"name2\""
           + "\r\n\r\n" + name2 + "\r\n"
           + "--" + boundary+"\r\n"
           + "Content-Disposition: form-data; name=\"name3\""
           + "\r\n\r\n" + name3 + "\r\n"
           + "--" + boundary+"\r\n"
           + "Content-Disposition: form-data; name=\"name4\""
           + "\r\n\r\n" + name4 + "\r\n"
           + "--" + boundary+"\r\n"
           + "Content-Disposition: form-data; name=\"name5\""
           + "\r\n\r\n" + name5 + "\r\n"
           + "--" + boundary+"--\r\n";


var n = multipart.length;
           
context.setVariable("multipart", multipart);
context.setVariable("boundary", boundary);
context.setVariable("multipartLength", n);

2 - put my multipart on a Assign Message Policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Template-Multipart">
    <DisplayName>Assign Template Multipart</DisplayName>
    <Properties/>
    <Add>
        <Headers>
            <Header name="Content-Type">multipart/form-data; boundary={boundary}</Header>
        </Headers>
    </Add>
    <Set>
        <Payload variablePrefix="@" variableSuffix="#">
            @multipart#
        </Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="request"/>
</AssignMessage>

I hope I could help,

Huu Hoa Nguyen

Hey @huu-hoa-externe.nguyen, I like this!

I'm trying to figure out how to correctly calculate the Content-Length for a request sent to the back end that has an image.

I want to just change the values of the form data variables and pass the image part along. Apigee seems to encode the message and does not set the Content-Length when I write the request back. Postman and curl report different values, far off from original.

Have you set Content-Length correctly in your messages, including those with images?

This works if all parts are text in a JS policy:

var body = context.getVariable("request.content");
body = body.replace('name="imagecategory"', 'name="imageCategoryAdd10chars"');
body = body.replace('name="uploaderid"', 'name="uploaderId"');
context.setVariable("request.content", body);
context.setVariable("request.header.X-Content-Length", body.length);

But not if an image is a part.

Suggestions?

Hey Kurt ,

Did you figure this out ?

On recieving the multipart message , I just did a getVariable(request.body) and setVariable(request.body) and my backend behaves differently. I dont even make a change. Did you figure out what APIGEE does while encoding and decoding this message ?

Thank you for your help.

@AlayVakil I have not solved this situation, and haven't looked at it since then. Will have to go back and check my test scripts, maybe over the weekend.

Sure , Thank you 😄