Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

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 5,536
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