Creating an ArrayBuffer and sending it as a file (attachment)

Hello there, hope you are doing well :slightly_smiling_face:
This time I would like to ask (after few hours of research), is there a way to initialize ArrayBuffer object which weights for like 5Kb and send it as attachment(simply put - file) in my POST request?

So far I have:

let buffer = new ArrayBuffer(5000)

    let attach = http.file(buffer, 'attach'+randomIntBetween(0,1000000)+'.dat', 'multipart/form-data')
    
    let res = http.post(`https://${__ENV.host}/upload`, attach);
    check(res, {'load attach': (r) => r.status === 200
}) || errorRate.add(1);

But it does really feel like I’m missing something here.
Here what I get:
ERRO[0003] GoError: unknown request body type http.FileData
I’ve re-read k6 articles about http.file multiple times, yet enlightenment still didn’t reach me.
Thank you a lot in advance!

Hello @Nesodus,
unfortunately, that is an expected error in this case.

As mentioned in the data-uploads docs:

When passing a JS object as the body parameter to http.post(), or any of the other HTTP request functions, where one of the property values is a FileData a multipart request will be constructed and sent.

In the case of FileData, you should pass to http.post an object with a property with the FileData assigned.

I’m reporting here a simpler version of the example from the doc:

export default function () {
  var data = {
    field: 'this is a standard form field',
    file: http.file(binFile, 'test.bin'),
  };

  http.post('https://example.com/upload', data);
}

I agree that passing directly the FileData generated from http.file could simplify the ux. I added an issue for tracking this feature request.

1 Like

Hello, @codebien
Thank you for a explanation, going to try this out.

yep, worked just fine, thank you!