How do i send multipart-data to my request ?

Not applicable

Hi everyone,

I want to send request with content-type "multipart/form-data", but i don't find anything for this subject in documentation. I try with Assign messag policy, with set a payload with this type of content-type, but it's not working...

For example, the request need looks like this :

sent request like this one : POST /api/rest/email/send HTTP/1.1 Host: edf-smartpush.aw.atos.net Authorization: Bearer ebcAahMk58

Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

----WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="sender"

noreply@mail.fr

----WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="recipientAddress"

test@mail.com

----WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="htmlContent" <html><body>Test et test et test</body></hmtl>

----WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="noReply"

true

----WebKitFormBoundary7MA4YWxkTrZu0gW

Content-Disposition: form-data; name="replyToPlatform"

false

----WebKitFormBoundary7MA4YWxkTrZu0gW

Thanks for your support !

Solved Solved
1 11 4,887
1 ACCEPTED SOLUTION

Below code can be used to generate the multipart payload from JSON

var jsonFormData = {
    'from': "'API  Support'<superteamxxx@google.com>",
    'html': "Dear API Support Team,<br/><br/>Below IP address has been detected exceeding allowed request limit <br/><br/>   Thanks,<br/>  API Team ",
    'to': "johncharterxxx@gmail.com",
    'subject': "Alert, Exceeding Allowed API Request Limit"
};

function getMultipartPayload(inputJson, boundary){
    var formPayload = "";
    var keys = Object.keys(inputJson);
    keys.forEach(function (key){
        formPayload  = formPayload + "------"+boundary+"\r\n"+"Content-Disposition: form-data; name=\""+key+"\"\r\n\r\n"+inputJson[key]+"\r\n"
    });
    formPayload+= "------"+boundary+"--";
    return formPayload;
}

//Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
//Use the same boundary value as in headers
var finalMultipartPayload = getMultipartPayload(jsonFormData, "WebKitFormBoundary7MA4YWxkTrZu0gW");

View solution in original post

11 REPLIES 11

Dear @huu-hoa-externe.nguyen , Welcome to Apigee Community 🙂

Thanks !

hope you can help me for this issue 🙂

@huu-hoa-externe.nguyen

Is your API call failing ? Are you getting any errors ?

See similar question asked here, let us know if it helps.

My API not working,

I use a mail web service as backend. For send mail i need to sent to the webservice mail's content in a request looks like my example.

but i don't find a possibility in some policy to create this type of request.

@huu-hoa-externe.nguyen

, Help me understand, Would you like to construct multipart/formdata request in Apigee proxy instead of collecting same from original request ?

I already seen this post. If my understanding is good, this post talking about how we can handling multipart in response. But my issue is when i want sent a request with multipart.

Sorry if i not talking clearly, english is not my first langage 🙂

My forkflow is this one :

- I receive in request a JSON object,

- I collect information to create a request to other webService, who sending mail.

But the webService need to receive request with content-type multipart/form-data. I don't find how i can construct this type of request.

Thanks for your help 🙂

Got it, Thank you for more details. Indeed, it's an interesting use case & great question. Let me think about it & come up with a solution. Can you share the web service documentation if any so that I can play with same if it is public?

Sorry, unfortunately it's a private webService, with basic Authentication and Bearer Authentication 🙂

But in my example you have the request that we need send

Below code can be used to generate the multipart payload from JSON

var jsonFormData = {
    'from': "'API  Support'<superteamxxx@google.com>",
    'html': "Dear API Support Team,<br/><br/>Below IP address has been detected exceeding allowed request limit <br/><br/>   Thanks,<br/>  API Team ",
    'to': "johncharterxxx@gmail.com",
    'subject': "Alert, Exceeding Allowed API Request Limit"
};

function getMultipartPayload(inputJson, boundary){
    var formPayload = "";
    var keys = Object.keys(inputJson);
    keys.forEach(function (key){
        formPayload  = formPayload + "------"+boundary+"\r\n"+"Content-Disposition: form-data; name=\""+key+"\"\r\n\r\n"+inputJson[key]+"\r\n"
    });
    formPayload+= "------"+boundary+"--";
    return formPayload;
}

//Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
//Use the same boundary value as in headers
var finalMultipartPayload = getMultipartPayload(jsonFormData, "WebKitFormBoundary7MA4YWxkTrZu0gW");