62

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"
}
dan1st
  • 12,568
  • 8
  • 34
  • 67
Justin Thompson
  • 631
  • 1
  • 5
  • 6

17 Answers17

120

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

Hasiya
  • 1,298
  • 1
  • 7
  • 7
20
  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.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
14

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

Jesper N
  • 2,115
  • 4
  • 23
  • 32
Sanjib Nath
  • 149
  • 1
  • 2
10

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.

M.A.Naseer
  • 343
  • 3
  • 9
Shoyeb
  • 101
  • 1
  • 2
5

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!

Akash Verma
  • 638
  • 1
  • 11
  • 15
1

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

Justin Thompson
  • 631
  • 1
  • 5
  • 6
1

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

vaquar khan
  • 10,864
  • 5
  • 72
  • 96
1

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

1

If none of the abovementioned solutions work for you (as it was for me) take a second look at the HTTP Header Manager. I had empty header line there enter image description here Removing it solved the problem

darth0s
  • 165
  • 2
  • 13
1

I was facing the similar issue, the size of my request body was also huge so along with updating content-type as application/json, i gave content encoding as utf-8 and it worked perfectly fine. enter image description here

0

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.

Ozgur Kaya
  • 253
  • 2
  • 7
  • I have run it in the body data before. I just tried with it set as java but that didnt work. If my token was invalid it would give me a 401 instead of the 400. – Justin Thompson May 11 '16 at 21:30
  • could you try to clean content encoding? I could say We use json requests only with post data, parameters are not working for my api. – Ozgur Kaya May 11 '16 at 21:37
0

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

TGU
  • 181
  • 2
  • 7
0

Insert your json in body data field.

In your HTTP Header Manager set the headers stores to:

"name" : Content-Type

"value": application/json

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0

Yes, we need to pass the Content-Type header. I faced the same issue and it took nearly a day to figure out until I came across this link - https://www.blazemeter.com/blog/performance-test-web-services

Header:

Name: Content-Type Value: application/json;charset=utf-8

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Abhilash
  • 457
  • 5
  • 9
-1

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

Suman g
  • 477
  • 3
  • 14
-1

Setting the HTTP header manager fixed the issue for me

Anilal
  • 17
  • 1
  • 1
    Could you elaborate a little more please? – Robert Oct 04 '18 at 16:22
  • Normally it would be really good if any answer would take the time to explain how this HTTP Header Manager was set, where from, and what the contents were. This way this would be a more useful answer. – esaruoho Feb 20 '20 at 12:56
-1

HTTP Header Manager: Name: Content-type and Value: application/json has resolved issue.

Fazlul Hoque
  • 119
  • 1
  • 2