How do I calculate the content-length for a multipart request that includes an image?

In my case, I want to just change the names of the form data variables in the request and pass the image part along to the backend unmodified.

Apigee seems to do something to the request (expands and changes image part) when I access it via the `request.content` variable and write the request back. Postman and curl report different values, far off from original.

I have to explicitly set the Content-Length, because an Assign Message after the JS policy doesn't do it, although that would be nice if it did.

This JS policy works if all parts of the form are text (test using curl without -F):

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 sent, which makes sense since JS string.length probably isn't working right on the image part.

So a couple of things:

  1. How can I manipulate the multipart form variable names without messing up the image part?
  2. How do I calculate the correct Content-Length?

I saw these related discussions:

Attached is a simple proxy I've been using to test things, a small image and the curl command:

curl -v -X POST -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -H "Cache-Control: no-cache" -F "imagecategory=Color swatches" -F "uploaderid=up12345" -F "file=@pink.png" "http://YOURORGNAME-test.apigee.net/validate-multipart-form"

The request Content-length is 4136, the curl response is 6680 and my calculated request is 3955 and the response is 3966 (which shows I've increased by 10, but less than the original request).

Any suggestions?

0 1 5,498
1 REPLY 1

@Kurt Kanaskie

were you able to solve above issue?