Gatling Post ignores Header



我正在使用Gatling对应用程序进行一些负载测试。

因此,我必须从一个带有application/x-www-form-urlencoded头的keycloft实例请求一个令牌。

private val httpRequest = http("Get access token")
.post("https://********/auth/realms/mds/protocol/openid-connect/token")
.asFormUrlEncoded
.formParam("client_id", "********")
.formParam("client_secret", "********")
.formParam("grant_type", "client_credentials")
val getAccessToken = exec(
httpRequest
.check(status.is(200))
.asJson
.check(jsonPath("$.access_token").saveAs("access_token"))
)

钥匙斗篷返回一个415因为这:

=========================
HTTP request:
POST https://********/auth/realms/mds/protocol/openid-connect/token
headers:
accept: application/json
host: ********
content-type: application/json
content-length: 103
body:FormUrlEncodedRequestBody{contentType='application/json', charset=UTF-8, content=client_id=********&client_secret=********&grant_type=client_credentials}
=========================
=========================
HTTP response:
status:
415 Unsupported Media Type
headers:
....
body:
{"error":"RESTEASY003065: Cannot consume content type"}

为什么Gatling忽略了我的内容类型标头?

调试器显示变量httpRequest具有正确的标头。什么将标题更改为application/json

您发布的formParams将生成用application/x-www-form-urlencoded编码的正文,然后您试图将生成的关联content-type标头覆盖到application/json

这是不合理的,导致服务器无法解码正文,因为content-type不匹配。

如果您想发送JSON,请发送带有正文的JSON负载,而不是表单。

经过几天的搜索,我发现了这个错误。

asJson不将响应转换为Json,而是设置内容类型和Accecptapplication/json标头

最新更新