How to specify content type for individual formdata elements

Is it possible to use something like Blob in K6, in order to have form data elements with different content types? i.e one is a file (easy enough), another is json. Each would have their own respective content types.

Something like:

const fd = new FormData();
   fd.append('bookInfo', new Blob([JSON.stringify({
        name: "Book",
        quantity: "12"
    })], {
        type: "application/json"
    }));
  fd.append('testFile', http.file(someFile, 'somefil.txt', 'application/octet-stream'));

Hi @jclarknet, welcome to the forum and sorry for the slow reploy :bowing_man:

It seems to not have been documented but you can just append an object with keys:

  • data - which will be the data you want
  • filename - you can skip that if you don’t want filename set.
  • content_type - with the content type you want, if not provided it will be application/octet-stream

You can see it done here

Hi, did you find a solution for this? because I am facing the same problem, I followed the solution provided by @mstoykov , which worked but I got an error , I will add my code here as well.

const fd = new FormData();
    fd.append("version", number);
    fd.append("file",{data: new http.file(binFile), filename: 'test.bin',content_type: 'application/octet-stream'});
    fd.append("firmwareId", "b5b51538-72b2-433f-b873-d65abf04b4b2");```

OUPUT:

execution: local
script: tester.js
output: -

scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
* default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

INFO[0017] number of devices: 1 organizationName: Pr Organazation t1 source=console
ERRO[0018] Uncaught (in promise) TypeError: Value is not a constructor

 ✓ status code: 200

Hi @Zahida.Parveen,

This topic is 1year+ old and unlikely to get a new response.

Also, your errors seem unlikely to be directly related to the topic at hand.

I would recommend opening a new topic with more info in order for somebody to be able to help you.

Hey @Zahida.Parveen , it’s been a while since I wrote this, but below is the code that worked for me:

fd.append('someFile', http.file(someFile, 'myfile.bin', 'application/octet-stream'));
 
const res = http.post('https://someurl', fd.body(), {
    headers: { 'Content-Type': 'multipart/form-data; boundary=' + fd.boundary },
});
3 Likes

@jclarknet Thankyou…