How to upload image without using CLI and open() function

In Load Imapct LUA script we could store image in a variable(base 64 format) and add data in http.post by encoding using base64.encode(data) and passing parameter base64_encoded_body. Want to know is there similar way in K6 or alternate way to store binary data so we could run script from webconsole instead of CLI.

Hi, welcome to the forum :slight_smile:

I think you might’ve run into an edge case that’s currently not well supported in the new k6 Cloud, mainly because of poor binary support. See issues #1375 and #1020.

Because encoding.b64decode() returns a string insted of bytes, it’s not really usable for this scenario. I tried storing a base64 string and passing it to a manually constructed multipart/form-data request, something like:

import http from 'k6/http';

const imgData = 'iVBORw0...QAAAABJRU5ErkJggg==';

export default function () {
  let resp = http.post(
    'http://localhost:9000/post',
    '--boundary\r\nContent-Disposition: form-data; name="file"; filename="test.png"\r\n' +
    `Content-Type: image/png\r\nContent-Transfer-Encoding: base64\r\n\r\n` + imgData + '\r\n--boundary--\r\n',
    { headers: { 'Content-Type': 'multipart/form-data; boundary=boundary' } }
  );
};

… but for this to work, your server would have to decode the base64, which is not ideal.

Feel free to experiment with this approach, and let us know if you make progress, but for now this remains a backlog issue that will likely be worked on in the near future.

1 Like