apideck-libraries/postman-to-k6

The converted post api return 415 in k6 running, it can work well in postman, please do me a favor, thanks a lot.

BrendaAspen opened this issue · 2 comments

The response code is 415 on the below API when running it by k6.

It works well in postman, the response code is 201.

Could you give me some helps on it? Thanks a lot.

postman[Request]({
name: "saveAgentWithOneTrainingRange",
id: "4836e494-b613-44c1-b650-bacf86f258d4",
method: "POST",
address:
"http://10.148.82.149/demo/APM/Data/api/MachineLearningAgents",
data:
'{"Id":"AA-1","Name":"AA-1","Description":"test","FkSensorGroup":40,"AgentType":"GMM","MaxIterations":20,"AgentCategory":1,"FkGranularity":21,"Attributes":"{\"profile\":{\"ActionType\":\"train\",\"AgentAlgo\":\"GMM\",\"FilterLength\":3,\"MinAlertDuration\":2,\"EventThreshold\":0.5,\"OperatingState\":3,\"DistanceType\":\"euclidean\",\"IterDroprRate\":5,\"Iterations\":200,\"AgentType\":\"anomaly\",\"TrainRange\":[[\"2013-06-18T17:31:58.000Z\",\"2013-12-18T06:12:56.000Z\"]]}}"}',
headers: {
Accept: "application/json, text/plain, /",
Authorization:
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJBcHBsaWNhdGlvbiI6IkFzcGVuIE10ZWxsIiwiUGVyc2lzdGVuY2UiOiJQcmltYXJ5IiwidW5pcXVlX25hbWUiOiJBZG1pbiIsInByaW1hcnlzaWQiOiIyIiwicm9sZSI6IkFkbWluaXN0cmF0b3IiLCJuYmYiOjE2NDkzNzk3MTYsImV4cCI6MTY0OTczOTcxNiwiaWF0IjoxNjQ5Mzc5NzE2LCJpc3MiOiJBc3BlbnRlY2hNdGVsbCJ9.lVdWs-wrMpOHA18OSWgqqKP8BT0mT9YVIKHm2Kbu9ks",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36",
"Content-Type": "application/json"
},
post(response) {
console.log("/api/MachineLearningAgents-response code:", pm.response.code);
pm.test("Success to add sensor role mapping on the sensor group on asset.", function () {
pm.expect(pm.response.code).to.be.oneOf([201]);
});
}
});

Postman sets the "content-headers" based on detection while in K6, the content-type header defaults to "application/x-www-form-urlencoded" if no content-type header is set.

In this discussion thread,we have described possible options to overcome your issue.

Thank you for your help @thim81
I did try with postman-to-k6 collection.json --k6-params k6-params.json -o k6-script.js, but the same error will pop up.
So I update all post api from postman script to k6 script and it works well.

payload = JSON.stringify({
"Id": "k6-AA-Id-" + randomNumber,
"Name": "AA-Name-" + randomNumber,
"Description": "added by k6 script",
"Attributes": "{"profile":{"ActionType":"train","AgentAlgo":"GMM","FilterLength":3,"MinAlertDuration":2,"EventThreshold":0.5,"OperatingState":3,"DistanceType":"euclidean","IterDroprRate":5,"Iterations":200,"AgentType":"anomaly","TrainRange":[["2013-06-18T17:31:58.000Z","2013-12-18T06:12:56.000Z"]]}}"
});
// console.log("payload:", payload);
params = {
headers: {
Accept: "application/json, text/plain, /",
Authorization:
"Bearer " + token,
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36",
"Content-Type": "application/json"
},
};
url = WAB_BASEURL + "/Data/api/MLAgents";
res = http.post(url, payload, params);
);

Thanks again for your hand, and it gave me an idea on how to resolve the problem.