# Cann't upload a file to api

**URL:** <https://community.grafana.com/t/cannt-upload-a-file-to-api/99603>\
**Category:** Grafana k6\
**Created:** [December 13, 2021, 9:52pm UTC](https://community.grafana.com/t/cannt-upload-a-file-to-api/99603 "2021-12-13T21:52:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steel\_tear](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Steel\_tear](https://community.grafana.com/u/Steel_tear)\
**Post date:** [December 13, 2021, 9:52pm UTC](https://community.grafana.com/t/cannt-upload-a-file-to-api/99603/1 "2021-12-13T21:52:48Z")

</div>

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!

```auto
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–

---

<div class="post-metadata">

**Author:** ![oleiade](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/oleiade/32/35836_2.png) [@oleiade](https://community.grafana.com/u/oleiade)\
**Post date:** [December 20, 2021, 8:31am UTC](https://community.grafana.com/t/cannt-upload-a-file-to-api/99603/3 "2021-12-20T08:31:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![oleiade](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/oleiade/32/35836_2.png) [@oleiade](https://community.grafana.com/u/oleiade)\
**Post date:** [December 20, 2021, 10:11am UTC](https://community.grafana.com/t/cannt-upload-a-file-to-api/99603/4 "2021-12-20T10:11:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steel\_tear](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Steel\_tear](https://community.grafana.com/u/Steel_tear)\
**Post date:** [January 17, 2022, 12:11pm UTC](https://community.grafana.com/t/cannt-upload-a-file-to-api/99603/5 "2022-01-17T12:11:06Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![oleiade](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/oleiade/32/35836_2.png) [@oleiade](https://community.grafana.com/u/oleiade)\
**Post date:** [January 17, 2022, 12:44pm UTC](https://community.grafana.com/t/cannt-upload-a-file-to-api/99603/6 "2022-01-17T12:44:50Z")

</div>

No worries, glad you found a solution!

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