Jmeter does not send JSON data in POST

Justin Thompson picture Justin Thompson · May 11, 2016 · Viewed 46.3k times · Source

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.

1

I am just trying to send {"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"
}

Answer

Hasiya picture Hasiya · Jun 12, 2018

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

  • 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.

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

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