Http.post request multiform data upload

Hello,
We are using K6 for performance testing. In the pipeline we are pulling latest docker image. We need to call a http.post request and we need to pass a multipart data with a pdf file as input. Following is the code we are using:

const binFile = open(‘test.pdf’, ‘application/octet-stream’);

const fd = new FormData();

    var params = {
            headers: {
                'content-type': undefined, // 'multipart/form-data; boundary=----WebKitFormBoundaryOo98grEHpbmFeVXs',
                'Authorization': 'Bearer '+ token()
            },
            tags: {
                name: "Upload Document"
            }
        };     

fd.append(‘content’, http.file(binFile), ‘test.pdf’, ‘application/octet-stream’));

console.log("filename: " + fd.filename); // This is coming undefined
console.log("contenttype: " + fd.content_type); // This is coming undefined

const res = http.post(url, data, params);

Here filename in the formdata is undefined and so this file is not being passed with the request.

Can you please guide what we are doing wrong here?

Thanks & Regards,
Madhura

Hi @MVB,

I’m happy to help you :slight_smile:

The properties filename and content_type are not the FormData properties (fd variable in the example from above). See the code of the FormData polyfill for k6.

const res = http.post(url, data, params);

The other thing that I noticed is that there is no declaration of the data used in the POST request.

But, there are a few examples of the file uploads that I believe can help Multipart request (uploading a file) and Advanced multipart request.

Let me know if that helps,
Cheers!

Thanks a lot for the response.

The data in const res = http.post(url, data, params);, I mistakenly put it wrong while copying code.
const res = http.post(url, fd.body(), params);

My issue is resolved, The problem was with boundary in header, It should be set to fd.boundary. I could figure it out b the file which you sent,
Thanks for the help.

Regards,
Madhura

1 Like