I need to upload the PDF file in the body of the post request through form data.
In addition to pdf file we have other key value pairs as well in the form data.
Can some one please guide me how to achieve this in k6.
I am writing the below code, but i am not able to get the proper response.
import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js';
let File= open("./Contentfactorytest.pdf");
let Obj = new FormData();
Obj.append('files', http.file(File));
let response;
const body = {
caseNumber: '122222222',
fileName: 'Contentfactorytest.pdf',
fileId: '12345678',
file: Obj.body()
};
export default function () {
group('Test case 1', () => {
response = http.post(http://sampleapi-outbound-doc/upload', body);
});
};
Hi @Pragadheshwaran
Apologies for the delay. If I understand correctly your requirements, I believe you could use the advanced example in Data Uploads. This works for me:
import http from 'k6/http';
import { sleep, check } from 'k6';
import { FormData } from 'https://jslib.k6.io/formdata/0.0.2/index.js';
const file = open('./Contentfactorytest.pdf')
export default function () {
const url = 'https://httpbin.test.k6.io/post';
const fd = new FormData();
fd.append('caseNumber', '122222222');
fd.append('fileId', '12345678');
fd.append('fileName', 'Contentfactorytest.pdf');
fd.append('file', http.file(file, 'Contentfactorytest.pdf', 'application/pdf'));
const res = http.post(url, fd.body(), {
headers: { 'Content-Type': 'multipart/form-data; boundary=' + fd.boundary },
});
check(res, {
'is status 200': (r) => r.status === 200,
});
}
I hope this helps. Cheers!