How to do multipart request with two xml files

Hello, I want to send a multipart request where I have two xml files to be send in request body form data using http.post API. How do I do that?
I am trying with following code but I get

Request Failed error="unknown request body type func(goja.FunctionCall) goja.Value' exception.

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

const job_config= open('../load-testing/request-files/job.conf-unmerge.xml')
const data= open('../load-testing/request-files/request-data.xml')

export const options = {
    vus: 1,
    duration: '1s',

}

export default function () {
    const url = 'https://***';

    const fd = new FormData();
    fd.append('content', http.file(job_config));
    fd.append('content', http.file(data));

    const params = {
        headers: {
            'Content-Type': 'multipart/form-data; boundary=' + fd.boundary,
            'Authorization': 'Basic ****',
        }
    }

    const res = http.post(url, fd.body, params);

    check(res, {
        'is status 200': (r) => r.status === 200,
    });

    sleep(1);
}

Hi @shobharawat

Welcome to the community forums :wave:

I slightly changed the example based on our data uploads documentation and this seems to work. Can you give it a try?

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

const job_config = open('./job.conf-unmerge.xml')
const data = open('./request-data.xml')

export const options = {
    vus: 1,
    duration: '1s',

}

export default function () {
    const url = 'https://httpbin.test.k6.io/post';

    const fd = new FormData();
    fd.append('xml', http.file(job_config, 'job.conf-unmerge.xml', 'text/xml'));
    fd.append('xml', http.file(data, 'request-data.xml', 'text/xml'));

    const res = http.post(url, fd.body(), {
        headers: {
            'Content-Type': 'multipart/form-data; boundary=' + fd.boundary,
            'Authorization': 'Basic ****',
        },
    });
    check(res, {
        'is status 200': (r) => r.status === 200,
    });

    sleep(1);
}

I hope this helps.

Cheers!

Thank you very much, it worked.

1 Like