37
votes

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"
}
12
I have the Content-Type set as application/json. I tried it in the body data but that didnt work either.Justin Thompson

12 Answers

70
votes

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

15
votes
  1. Make sure your HTTP Header Manager is configured to send Content-Type header 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 Tree listener (all 3 tabs) and Postman

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

11
votes

This solution from Dmitri T, really worked for me.

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

9
votes

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.

3
votes

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"

Now hit. Should do!

1
votes

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

1
votes

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

enter image description here

1
votes

I had given HTTP/1.1 415 Unsupported Media Type

After deleting space before of HTTP Header Manager ,It got 200 and worked like a charmenter image description here

0
votes

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.

0
votes

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

0
votes

Setting the HTTP header manager fixed the issue for me

0
votes

In addition to many said above, make sure that the copy/paste of Accept, Content-Type from website doesn't have a trailing space! It costs me many hours to figure it out. Just a caution.

Just edit to add that if that mistake happens, return code is 415: Unsupported Media Type