K6 替换 Content-Type 标头?

huangapple go评论132阅读模式
英文:

K6 replacing Content-Type header?

问题

你好,我有一个k6脚本。k6脚本执行了一个POST请求。在POST请求中,我还设置了请求体和请求头。然而,k6忽略了头部,并添加了不同的头部值。请看以下代码:

import http from 'k6/http';

export const payload = JSON.stringify({
    firstField: 'Sample value'
});

export const params = {
   headers: {
      'Content-Type': 'application/json',
    }
};

export const options = {
  discardResponseBodies: false,
  scenarios: {
    defaultOperationPriority: {
      executor: 'constant-vus',
      exec: 'defaultOperationSubPriority',
      startTime: '0s',
      vus: 1,
      duration: '3s',
    },
    configuredOperationPriority: {
      executor: 'constant-vus',
      exec: 'configuredOperationSubPriority',
      startTime: '1s',
      vus: 1,
      duration: '3s',
    },
  },
};

export function defaultOperationSubPriority() {
  http.post('http://10.96.59.144:80/SccTestService/v1/requestMirror',  params);
}

export function configuredOperationSubPriority() {
  //  http.get('http://10.96.59.144:80/SccTestService/v1/id');
}

以下是输出的一部分:

group= iter=360 request_id=28c20476-cea2-494c-4792-9400643de471 scenario=defaultOperationPriority source=http-debug vu=2
INFO[0001] Request:
POST /SccTestService/v1/requestMirror HTTP/1.1
Host: 10.96.59.144:80
User-Agent: k6/0.43.1 (https://k6.io/)
Content-Length: 50
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip

我能看到k6设置了Content-Type和Accept-Encoding。我想用'Content-Type': 'application/json'替代它们。有什么方法可以实现吗?谢谢。

英文:

Hi I am having a k6 script. The k6 script do a post request . in the post request i am also setting the body and header .
however the headers are ignored by k6 and k6 adding a different header value. please see the code

import http from 'k6/http';

export const payload = JSON.stringify({
    firstField: 'Sample value'
});

export const params = {
   headers: {
      'Content-Type': 'application/json',
    }
};

export const options = {
  discardResponseBodies: false,
  scenarios: {
    defaultOperationPriority: {
      executor: 'constant-vus',
      exec: 'defaultOperationSubPriority',
      startTime: '0s',
      vus: 1,
      duration: '3s',
    },
    configuredOperationPriority: {
      executor: 'constant-vus',
      exec: 'configuredOperationSubPriority',
      startTime: '1s',
      vus: 1,
      duration: '3s',
    },
  },
};

export function defaultOperationSubPriority() {
  http.post('http://10.96.59.144:80/SccTestService/v1/requestMirror',  params);
}

export function configuredOperationSubPriority() {
//  http.get('http://10.96.59.144:80/SccTestService/v1/id');
}

the following is the part of the output

 group= iter=360 request_id=28c20476-cea2-494c-4792-9400643de471 scenario=defaultOperationPriority source=http-debug vu=2
INFO[0001] Request:
POST /SccTestService/v1/requestMirror HTTP/1.1
Host: 10.96.59.144:80
User-Agent: k6/0.43.1 (https://k6.io/)
Content-Length: 50
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip

i could see the k6 is setting Content-Type & Accept-Encoding. i want to replace them with 'Content-Type': 'application/json'

any idea how could i achieve it
thanks

答案1

得分: 0

payload缺失

英文:
  http.post('http://10.96.59.144:80/SccTestService/v1/requestMirror', payload , params);

payload was missing

huangapple
  • 本文由 发表于 2023年3月4日 03:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定