Jmeter does not send JSON data in POST
Asked Answered
L

17

68

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"
}
Lyra answered 11/5, 2016 at 17:8 Comment(1)
I have the Content-Type set as application/json. I tried it in the body data but that didnt work either.Lyra
B
130

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

Brierwood answered 12/6, 2018 at 3:59 Comment(0)
C
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.

Cobbs answered 11/5, 2016 at 17:31 Comment(0)
S
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".

Specs answered 1/3, 2019 at 12:45 Comment(0)
S
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.

Stench answered 19/9, 2018 at 5:19 Comment(1)
Content Encoding to utf-8 worked for me! Thanx!Saraann
E
6

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!

Eanes answered 11/5, 2020 at 12:27 Comment(0)
C
4

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

Collaborate answered 14/9, 2022 at 13:3 Comment(0)
T
3

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

Tolbert answered 24/8, 2022 at 10:48 Comment(0)
L
1

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

Lyra answered 12/5, 2016 at 14:35 Comment(0)
U
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

Unpromising answered 8/5, 2020 at 19:38 Comment(0)
B
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

Bishopric answered 15/2, 2021 at 22:55 Comment(0)
I
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

Illlooking answered 15/12, 2022 at 5:21 Comment(0)
M
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.

Mainspring answered 11/5, 2016 at 21:18 Comment(2)
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.Lyra
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.Mainspring
F
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

Festal answered 26/1, 2021 at 2:40 Comment(0)
Z
0

Insert your json in body data field.

In your HTTP Header Manager set the headers stores to:

"name" : Content-Type

"value": application/json

Zellner answered 10/9, 2021 at 12:2 Comment(0)
C
0

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

Chide answered 28/2, 2022 at 12:45 Comment(1)
This has already been mentioned in the other answers.Alfilaria
I
-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

Isochronism answered 12/5, 2016 at 9:26 Comment(0)
S
-1

Setting the HTTP header manager fixed the issue for me

Sedation answered 4/10, 2018 at 15:52 Comment(2)
Could you elaborate a little more please?Ruthful
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.Guitarfish

© 2022 - 2024 — McMap. All rights reserved.