Cann't upload a file to api

Hi guys! I need a help. I don’t understand why I’m not able to send a jpg file to api. And I kind of give up already… I have spent a lot of time already. It returns to me:

“error”: “kiosk_form_error”,
“error_description”: “undefined method `keys’ for nil:NilClass”.
The form requires key answers[30] + file.

Thank you!

import http from 'k6/http';
import { sleep } from 'k6';
import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js';

const img1 = open('bunch_of_gnomes.jpg', 'b');


export default function () {
    const fd = new FormData();
    fd.append('answers[30][]', http.file(img1, 'bunch_of_gnomes.jpg', 'image/jpg'));

  const res = http.post('https://somehost/submit', fd.body(),{
    headers: { 'Content-Type': 'multipart/form-data; boundary=' + fd.boundary}} );
  sleep(3);

  console.log(res.body)

How this request works in a browser:

------WebKitFormBoundary1uT9hs21weNI5y3d
Content-Disposition: form-data; name=“answers[30]”; filename=“DsI6yX_VYAAwXLz.0.jpg”
Content-Type: image/jpeg

------WebKitFormBoundary1uT9hs21weNI5y3d–

Hi @Steel_tear,

Thanks for reaching out. Just a quick heads up that I’m looking into it and will come back to you as soon as I have made progress.

Hey @Steel_tear

I’ve dug a little in your issue. As far as I can tell, it doesn’t seem that the error is not related to K6 itself, but rather to the request you’re sending not being what the server you’re load testing expects. Sourcegraph shows no result for kiosk_form_error, and if I’m not mistaken “undefined_method keys for nil:NilClass” is a Ruby/Ruby on Rails error.

It would be quite difficult for me to help you directly debug this issue without having access to your code/infrastructure. Thus, I would recommend, as a first step, that you inspect what the differences are between the requests you send through the browser (the inspector should help you achieve that), and the ones sent by k6 (k6 run --http-debug script.js will make K6 verbosely display every HTTP interactions. From there, I hope it should help you adjust your K6 load test script in accordance to address your issue.

Let me know if that’s helpful and if I can support you with anything else.

1 Like

Hi! I’m sorry for the delayed answer. I didn’t see the message. I fixed my problem with converting postman’s requests to k6’s code. Thank you --http-debug option, I didn’t know about this before. Thank you very much :slight_smile:

No worries, glad you found a solution!

Don’t hesitate to get back in touch if you need further assistance in the future :+1:

1 Like