K6: K6 Does not respect the boundary value placed in a Content-Type header

Created on 14 Nov 2018  路  5Comments  路  Source: loadimpact/k6

If you specify a Context-Type header for a multipart form with a specific boundary, it is ignored:

let parameters = {
        headers: {
            "Authorization": "someAuthToken",
            "Content-Type": "multipart/form-data; boundary=--myBoundary",
            "Accept-Encoding": "br, gzip, deflate"
        }
    };

When looking at the logged request, you get:

POST /some/url HTTP/1.1
Host: api.feat1.dev.imaginecurve.com
User-Agent: k6/0.22.1 (https://k6.io/);
Content-Length: 507
Accept-Encoding: br, gzip, deflate
Authorization: someAuthToken
Content-Type: multipart/form-data; boundary=--myBoundary
Cookie: device_view=full

--87154a0502b4f0b523bb4fc2571b3d87d741f6e7ca73baaad7f8ab5eb74a
Content-Disposition: form-data; name="foo"

foo content
--87154a0502b4f0b523bb4fc2571b3d87d741f6e7ca73baaad7f8ab5eb74a
Content-Disposition: form-data; name="bar"

bar content
--87154a0502b4f0b523bb4fc2571b3d87d741f6e7ca73baaad7f8ab5eb74a
Content-Disposition: form-data; name="_dummyFile"; filename="placeholder"
Content-Type: application/octet-stream


--87154a0502b4f0b523bb4fc2571b3d87d741f6e7ca73baaad7f8ab5eb74a--

Note that the boundary separator is not the one specified in the Content-Type. This results in clients being unable to parse the payload because it can't find the specified boundary.

I would suggest this is linked to #747

bug

All 5 comments

The workaround is to just not specify a Content-Type, K6 then automatically generates one with the correct boundary definition.

Thanks, you're right, we should fix this at the same time we fix https://github.com/loadimpact/k6/issues/747, since both issues are very connected!

In the case that I MUST change manually the 'Content-type' and then add a random boundary value doesn't work. My scenario is:

  • I receive from a flowContext my header params and the content-type is set automatically to 'application/json'
  • Since I'm testing an endpoint that requires a 'multipart/form-data' I have to manually change the header content-type and added a random boundary value.
let binPdf = open("../bin-pdf.pdf", "b");
param.headers['Content-Type'] = "multipart/form-data;boundary=random-boundary-value";
        var data = {
            file: http.file(binPdf, "bin-pdf.pdf", "application/pdf") 
        };
let res = http.post(url, data, params);

K6 still complaining of Missing initial multi part boundary

@IsabelaPastorini Could you try this workaround suggested in #1571?

Unfortunately you might need to build the entire body manually, or create it outside of k6 and load the binary that way.

This issue is in our backlog, but I can't say an ETA for a fix, as other issues currently have higher priority. Sorry for the inconvenience!

It worked fine! Unfortunately pdf file converted to base64 is to big, so I setUp random data like "123" to convert and worked, Thanks or the suggestion.
Since k6 pick the correct boundary-value from your file and return a header with this value, can we check if the header is present in the request and change it to the correct one? I take a look in the request.go, more specifically in
handleObjectBody := func(data map[string]interface{})

````

result := &parsedHTTPRequest{

聽 | url: &reqURL,
聽 | req: &http.Request{
聽 | Method: method,
聽 | URL: reqURL.URL,
聽 | Header: make(http.Header),
聽 | },

result.req.Header.Set("Content-Type", mpw.FormDataContentType())
````

Creates a new Header and then fill Contet-type with the correct value.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sdhoward picture sdhoward  路  3Comments

na-- picture na--  路  3Comments

kokokenada picture kokokenada  路  4Comments

na-- picture na--  路  4Comments

jrm2k6 picture jrm2k6  路  4Comments