Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jmeter does not send JSON data in POST

Tags:

json

post

jmeter

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" } 
like image 646
Justin Thompson Avatar asked May 11 '16 17:05

Justin Thompson


People also ask

How to send JSON data in Post request in JMeter?

To perform sending the JSON payload we need a different configuration. First, we need to add HTTP Header Manager into our HTTP Request in order to set Content-Type as application/json. We then put the JSON payload into the Body Data section of our HTTP Request Sampler.

Can you send JSON in post?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.

How to Post data in JMeter?

Create a Test Plan First For JMeter Post Requests Just right click on Test Plan and then click on the element by following this path: Threads (Users) -> Thread Group. Your Thread Group should be added now. And you can keep values in Thread Properties as they are.

How extract JSON data from JMeter?

JSON Extractor- It is a handy tool to execute JSON Path expressions against JSON responses and storing the result into a JMeter Variable. If the response of any request is in JSON then we can use this extractor. Let's see how can we use it in our script to extract the values from JSON.


2 Answers

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

like image 53
Hasiya Avatar answered Sep 22 '22 03:09

Hasiya


  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.

like image 45
Dmitri T Avatar answered Sep 19 '22 03:09

Dmitri T