Upload Content to API

I am trying to upload file (excel {xlsx}) to an API using k6.

I am using the K6 script
k6>process.js
this script call an api (Asp.net core API (5.0)).
The file(s) are uploaded but file uploaded file(s) are not correct.

To reproduce :

  1. Clone Repository (GitHub - HiteshDutt/K6ContentUpload), Checkout branch main
  2. In Directory API, open solution FileUpload.sln
  3. Run the project (web API) to expose endpoint.
  4. Navigate to main parent directory and change directory to k6
  5. in k6 directory please run process.js script using k6 run ./process.js
  6. uploaded files are found in API>FileUpload>Recources>Images

Using POSTMAN to upload files using the same endpoint works as expected. Please find postman export under directory name PostMan.
The Post action is used to send content of excel as binary data.

Seeking help to fix k6 script to upload the file.

Hi @hidutt. welcome the forum!

Thanks for the repo!

You wrapping the body in http.file means that it will make the body to be a multipart-form which is what you then dump to the file. You should just fileContent as the body.

edit: I have made you a PR with both the “fix” and a Dockerfile I needed to make so I can test it.

1 Like

Amazing. This works :slightly_smiling_face:

I am sure this would be part of any documentation but I could not find it.

Thanks for your quick support and help.

Hello, i’m a new to k6 and i have some issue.
I’m trying to send a POST request to upload xlsx files to an API. It’s doesn’t work with Postman as well.

When i try to display the fileContent from fileContent = open(“./files.xlsx”, “b”), its seems empty. And when i try to display the results body from res = http.post(url, fileContent, params) i have this error, “Cannot deserialize the current JSON object”.
I’m using the same script as below (process.js)

Thank you in advance for your help.

@kroukss please make a new post as this is over 1 year old. Preferably with more information - it seems like the script specifically wasn’t posted.

Thanks for your response. I made a new post with more information :slight_smile: