Justin Thompson
Justin Thompson

Reputation: 711

Jmeter does not send JSON data in POST

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

Upvotes: 70

Views: 124440

Answers (17)

Chetanti Mehta
Chetanti Mehta

Reputation: 19

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

Upvotes: 0

darth0s
darth0s

Reputation: 320

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

Upvotes: 4

Abhilash
Abhilash

Reputation: 557

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

Upvotes: 4

Fazlul Hoque
Fazlul Hoque

Reputation: 129

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

Upvotes: 0

fady manaa
fady manaa

Reputation: 5

Insert your json in body data field.

In your HTTP Header Manager set the headers stores to:

"name" : Content-Type

"value": application/json

Upvotes: 0

soufiane ELAMMARI
soufiane ELAMMARI

Reputation: 1029

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

Upvotes: 2

TGU
TGU

Reputation: 189

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

Upvotes: 1

Akash Verma
Akash Verma

Reputation: 847

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!

Upvotes: 6

vaquar khan
vaquar khan

Reputation: 11449

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

Upvotes: 1

Sanjib Nath
Sanjib Nath

Reputation: 149

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

Upvotes: 14

Shoyeb
Shoyeb

Reputation: 101

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.

Upvotes: 10

Anilal
Anilal

Reputation: 17

Setting the HTTP header manager fixed the issue for me

Upvotes: -1

Hasiya
Hasiya

Reputation: 1458

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

Upvotes: 135

Justin Thompson
Justin Thompson

Reputation: 711

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

Upvotes: 1

Suman g
Suman g

Reputation: 477

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

Upvotes: -1

Ozgur Kaya
Ozgur Kaya

Reputation: 253

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.

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168072

  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.

Upvotes: 20

Related Questions