【问题标题】:How to make a simple POST request in k6 with multiple custom headers如何在 k6 中使用多个自定义标头发出简单的 POST 请求
【发布时间】:2021-03-09 23:57:05
【问题描述】:

当我收到错误消息时,我无法让这个脚本终生工作:INFO[0001] {"error":{"code":2,"message":"Invalid or Missing X-App-Token", "文档":""}}

但是,带有此标头的 curl 请求可以正常工作。这是我们在整个项目中使用的自定义标头。我找不到任何传入多个标头的 k6 示例。 curl 请求也不需要接受标头,因此仅尝试传入 X-App-Token,没有更改。尝试使用逗号和括号。我相信我正确地传递了标题。

import encoding from 'k6/encoding';
import http from 'k6/http';
import { check } from 'k6';

export default function () {

var url = 'https://api.mycompany.com/test/scenarios/PropertyManager';

const params = {
headers: {
  'X-App-Token':'<proprietary string>',
  'accept':'application/json' 
},
timeout: 2000,
};

let res = http.post(url, params);
console.log(res.body);

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

}

【问题讨论】:

    标签: k6


    【解决方案1】:

    2 参数 http.post 将第二个参数视为 POST 数据,而不是 params

    您应该使用 3 参数 http.post:

    let res = http.post(url, {}, params);
    

    假设你发送一个没有 POST 数据的 POST 请求,否则修改空对象。

    【讨论】:

      猜你喜欢
      • 2012-01-20
      • 2013-01-24
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 2020-02-15
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多