Post multipart form data in k6, with a token

Hi all,

I’m trying to post with multi-part uploads.
The request has a header manger that contains a authorization token (“Bearer ”), a parameter that contains a json in text, and a pdf file to upload.

It works in JMeter for me, but not in k6.

In JMeter:

Http Header Manager:
Authorization: some_token

Http Request:
Method: post
Use multipart/form-data: yes

Payloads:

  1. “Parameters” tab:
    name: body
    value:
    Content-type: text/plain

  2. “Files Upload” tab:
    File Path: some_file_path
    Parameter name: document
    MIME Type: application/octet-stream

In K6
(reference: Data Uploads)

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

const my_pdf_file = open(‘./my_pdf_file.pdf’);
const my_token = some_token;

export default function () {

let data = { some json};
const fd = new FormData();
fd.append(‘body’, data, ‘text/plain’);
fd.append(‘file’,http.file(my_pdf_file,‘my_pdf_file.pdf’,‘application/octet-stream’));

const res = http.post(my_url, fd.body(),{
headers: {‘Authorization’: my_token, ‘Content-Type’: ‘multipart/form-data; boundary=’ + fd.boundary },
});

The server returns 403 (not authorized) error.

If I change ‘Content-Type’ to ‘applicaiton/json’ as shown below:
const res = http.post(my_url, fd.body(),{
headers: {‘Authorization’: my_token, ‘Content-Type’: ‘applicaiton/json’ },
});

The server returns 500 internal server error. It seems that the second code at least passed authorization part. But why?

k6 version:
k6 v0.46.0 (2023-08-14T13:23:26+0000/v0.46.0-0-gcbd9e9ad, go1.20.7, windows/amd64)

Could some one shed some light on this?

Thanks,
-Madison

Hi Madison - “applicaiton” or “application”?

It should be “application”. sorry for the typo.