multipart/form-data API request and add another form param to incoming request

We have multipart/form-data request with form parameter containing text and file. We want to add another text form param to this request. We are not able to figure out a way to be done using Javascript. Can someone help with suggestions about how it can be achieved ?


@dchiesa1 @ssvaidyanathan 

0 1 1,000
1 REPLY 1

Apigee does not have builtin policies to construct multipart/form-data requests.  The format is relatively simple, though, and you can construct your own form with just text/string concatenation, and assign the result to request.content.  Basically follow examples like this one, but.... rather than using XMLHTTPRequest() to send the form out, just assign the resulting string to request.content .  The JavaScript policy will work fine for this purpose. You just need to take care to get the boundaries right. The examples you can find online will be applicable here. 

JavaScript won't work if the form you are constructing includes a "binary" file - anything that is not UTF-8 text.  That would include a PDF file, ZIP file, DOCX file, PNG, and many others. Those, you cannot manipulate from within a JavaScript policy in Apigee. 

You said you want to "add" a field or two.  There's one nuance to that - you will need to parse the existing message to find the boundary string.  Also you need to strip the trailing double-dash "--" from the original message in order to be able to append additional form fields.  Then re-append the double-dash after the final boundary string. This should all be kinda obvious if you read up on the structure of multipart form-data requests. 

Your other option is to use the Java callout that can create multipart forms. https://github.com/DinoChiesa/Apigee-Java-Simple-MultipartForm

This custom java policy does the construction for you.  It does not "append" fields to an existing message. Instead it creates a brand new message, or parses an existing message.  If you are handy with Java, you could start with that callout and enhance it so that it can modify an existing multipart form request.  You can make an enhancement request too, but I cannot provide any "service level" guarantee on when I might be able to do that work. I could rush it if you are a really good friend :). but I have a day job, yknow how it is.