Jmeter 不会在 POST 中发送 JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37169153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:25:13  来源:igfitidea点击:

Jmeter does not send JSON data in POST

jsonpostjmeter

提问by Justin Thompson

I am trying to do a POST in jmeter with a json as the body data but I just get a 400 back. The URL I am sending to only accepts json. I have used the same curl in Postman and it worked just fine. I have tried putting the json in the parameters as a value with no name and that didnt work.

我正在尝试在 jmeter 中使用 json 作为正文数据进行 POST,但我只得到了 400。我发送到的 URL 只接受 json。我在 Postman 中使用了相同的 curl,效果很好。我曾尝试将 json 作为没有名称的值放入参数中,但没有奏效。

1

1

I am just trying to send {"uid":"jmtest","name":"newdevice"}.

我只是想发送 {"uid":"jmtest","name":"newdevice"}。

Sorry I cant post more links.

抱歉,我不能发布更多链接。

The sampler result is

采样结果是

Thread Name: QA test 1-1
Sample Start: 2016-05-11 11:50:19 MDT
Load time: 86
Connect Time: 51
Latency: 86
Size in bytes: 282
Headers size in bytes: 244
Body size in bytes: 38
Sample Count: 1
Error Count: 1
Data type ("text"|"bin"|""): text
Response code: 400
Response message: Bad Request
Response headers:
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 11 May 2016 17:50:19 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 38
Connection: keep-alive
X-Request-Id: 88339ee9-b74f-4e22-b581-e3124949d067
X-Runtime: 0.030877
HTTPSampleResult fields:
ContentType: application/json; charset=utf-8
DataEncoding: utf-8

And the request looks like

请求看起来像

/url redacted
POST data:
{"devices":{"uid":"jmtest","name":"newdevice"}
[no cookies]
Request Headers:
Connection: keep-alive
Accept: application/vnd.moneydesktop.v2+json
Content_Type: application/json
MD-SESSION-TOKEN: redacted
Content-Type: application/x-www-form-urlencoded
Content-Length: 46
Host: redacted
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_72)

The response data is

响应数据是

{
:   "status":"400",
:   "error":"Bad Request"
}

回答by Hasiya

To send a POST HTTP Request with the JSON Data inside the body, need to add,

要在正文中发送带有 JSON 数据的 POST HTTP 请求,需要添加,

  • HTTP Header Manger into your request and set the name as 'content-type' and value as 'application/json' this will attached into HTTP request header and what ever the data inside your request body will send as json format.
  • HTTP Header Manger 到您的请求中,并将名称设置为“content-type”,将值设置为“application/json”,这将附加到 HTTP 请求标头中,并且您的请求正文中的任何数据都将以 json 格式发送。

Image 1:Set HTTP Header Manager, enter image description here

图 1:设置 HTTP 标头管理器, 在此处输入图片说明

Image 2:Set HTTP Request Body Data enter image description here

图 2:设置 HTTP 请求正文数据 在此处输入图片说明

回答by Dmitri T

  1. Make sure your HTTP Header Manageris configured to send Content-Typeheader with the value of application/json
  2. Given you have samplers like "home page" and "login" it might be the case you're missing HTTP Cookie Manager
  3. The most straightforward way to see what's wrong is to capture requests sent by Postman (whatever it is) and JMeter by a sniffer tool like Wireshark, identify the differences and configure JMeter accordingly.
  4. Finally, it looks like you're using some developer snapshot so approach to pass JSON payload as 1st argument without name might not work. Try switching to "Body Data" instead.
  5. If above steps won't help update your question with screenshots of View Results Treelistener (all 3 tabs) and Postman
  1. 确保您的HTTP 标头管理器配置为发送Content-Type具有以下值的标头application/json
  2. 鉴于您有诸如“主页”和“登录”之类的采样器,可能是您缺少HTTP Cookie 管理器的情况
  3. 查看错误的最直接方法是通过Wireshark 之类的嗅探器工具捕获 Postman(无论是什么)和 JMeter 发送的请求,识别差异并相应地配置 JMeter。
  4. 最后,看起来您正在使用一些开发人员快照,因此将 JSON 有效负载作为没有名称的第一个参数传递的方法可能不起作用。尝试切换到“身体数据”。
  5. 如果以上步骤无助于使用查看结果树侦听器(所有 3 个选项卡)和邮递员的屏幕截图更新您的问题

Check out Testing SOAP/REST Web Services Using JMeterfor more tips.

查看使用 JMeter 测试 SOAP/REST Web 服务以获取更多提示。

回答by Sanjib Nath

This solution from Dmitri T, really worked for me.

Dmitri T 的这个解决方案真的对我有用。

Make sure your HTTP Header Manager is configured to send "Content-Type" header with the value of "application/json".

确保您的 HTTP 标头管理器配置为发送值为“application/json”的“Content-Type”标头。

回答by Shoyeb

I have faced the same issue and it was resolved by setting the value of Content Encoding to utf-8 in http request. Please try.

我遇到了同样的问题,通过在 http 请求中将 Content Encoding 的值设置为 utf-8 解决了这个问题。请尝试。

回答by Akash Verma

First thing you might wanna do is put the payload inside "Body Data" Instead of "Parameters"

您可能想要做的第一件事是将有效载荷放在“身体数据”而不是“参数”中

Then, add a config element "HTTP Header Manager" And add a parameter "content-type" With corresponding value "application/json"

然后,添加一个配置元素“HTTP Header Manager”并添加一个参数“content-type”和对应的值“application/json”

Now hit. Should do!

现在打。应该做!

回答by Justin Thompson

I figured out my problem. I had set Content_type instead of Content-Type so it was creating two content type headers.

我想出了我的问题。我设置了 Content_type 而不是 Content-Type 所以它创建了两个内容类型标题。

回答by Anilal

Setting the HTTP header manager fixed the issue for me

设置 HTTP 标头管理器为我解决了这个问题

回答by vaquar khan

Though Hasiya explain really well but missed how to find http request. If you chose http default will not see method option .

虽然 Hasiya 解释得很好,但错过了如何找到 http 请求。如果选择 http 默认不会看到方法选项。

enter image description here

在此处输入图片说明

回答by Ozgur Kaya

You must use body data, not parameters. cut and paste it the next tab on your request. Also you should change implementation to Java and check if your token is valid. Finally check your results from view results tree reporting item. It must be work for you.

您必须使用正文数据,而不是参数。根据您的要求将其剪切并粘贴到下一个选项卡。此外,您应该将实现更改为 Java 并检查您的令牌是否有效。最后从查看结果树报告项目中检查您的结果。它一定适合你。

回答by Suman g

As per your attached pic , You have pasted the request in queryparameters , the payload must be in body part

根据您附加的图片,您已将请求粘贴到查询参数中,有效负载必须在正文部分

check the screenshot you have payload in queryParam section

检查您在 queryParam 部分具有有效负载的屏幕截图