Getting "code":400 in k6, however the postman is able to run successfully/

,

Below is the example of my postman equivalent curl

 curl --location 'http://my-service/v3/legacy/import?family=WGeneric' \
--header 'X-TRS-AUTH-PERMS: {{auth_perms}}' \
--header 'X-Trs-Auth-Alias: {{auth_alias}}' \
--header 'X-APIKEY: <qwertyuiopasdfghjkl;>' \
--form 'file=@"/Users/xx/load-test/scripts/files/data.xls"'

output:

{
    "msg": "File successfully uploaded and submitted to avdownload",
    "sha256": "sdfghjksdfgh5678dhkhfkdsfdsfdsf",
    "success": true
}

Below is my k6 code, (I have put only the relevent part)

const filePath = '/Users/xx/load-test/scripts/files/data.xls';

// Read the file content during the initialization phase
const fileContent = open(filePath);
if (!fileContent) {
    console.error(`Error reading file content from ${filePath}`);
    // Abort the test or handle the error as needed
}

export default function () {
    const apikey = __ENV.API_KEY;

    const headers = {
        'X-APIKEY': apikey,
    };

    const response = http.request( "POST", baseURL, fileContent, {
        headers: headers,
    });

    check(response, {
        'status is 200': (r) => r.status === 200,
    });
    console.log(`Request URL: ${baseURL}`);
    console.log(`Request Headers: ${JSON.stringify(headers)}`);
    console.log(`Response Status Code: ${response.status}`);
    console.log(`Response Body: ${response.body}`);
    sleep(1);
}

the output of logs would be

INFO[0027] Response Status Code: 400                     source=console
INFO[0027] Response Body: {"@status":"error","code":400.......}

Hi @shvkumara

Welcome to the community forum :wave:

You are getting an HTTP400 error - Bad request.

I see in your curl command you have more headers like X-TRS-AUTH-PERMS and X-Trs-Auth-Alias which are missing in the k6 script. and it could also be related to how you upload the file.

I find it helpful to try to run the requests in debug mode and compare them (curl/postman and k6). And see if you would need to add those headers or any other data. I find there is a good example of this in:

If you compare the requests/responses from curl with what k6 is doing, you will probably spot what is missing in the requests and make sure you add that to the k6 script.

On the upload part of the data, I would have a look at the example at Data Uploads | Grafana k6 documentation.

I hope this helps :bowing_woman:

Cheers!

1 Like

@eyeveebee I have removed them in the question for easy understanding, I am paasing them as below

    const headers = {
        'X-TRS-AUTH-PERMS': auth_perms,
        'X-Trs-Auth-Alias': auth_alias,
        'X-APIKEY': apikey,
    };

@shvkumara

Could you try with:

    const response = http.request('POST', baseURL, {file: http.file(fileContent, filePath)}, {
        headers: headers,
    });

I hope it works

2 Likes

@bandorko Thank you, The below part worked.

{file: http.file(fileContent, filePath)}

This worked.

2 Likes