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.
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"
}
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,
Image 2:Set HTTP Request Body Data
Make sure your HTTP Header Manager is configured to send Content-Type header with the value of application/json
Given you have samplers like "home page" and "login" it might be the case you're missing HTTP Cookie Manager
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.
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.
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.
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".
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.
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!
I figured out my problem. I had set Content_type instead of Content-Type so it was creating two content type headers.
Though Hasiya explain really well but missed how to find http request.
If you chose http default will not see method option .
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 charm
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.
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
Insert your json in body data field.
In your HTTP Header Manager set the headers stores to:
"name" : Content-Type
"value": application/json
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
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
Removing it solved the problem
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.
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
Setting the HTTP header manager fixed the issue for me
HTTP Header Manager:
Name: Content-type and Value: application/json has resolved issue.
Related
I am trying to test a web API using JMeter. The web server interacts between JMeter and a database. The server is supposed to take parameters from the JSON in HTTP POST requests and parse them into queries.
The code of the web server in the relevent controller looks like this:
[HttpPost]
[Route("my_awesome_route_path")]
public async Task<List<QueryResult>> invoice([FromBody]InvoiceInfo invoiceInfo)
{
query = string.Format("INSERT INTO my_awesome_table (`my`,`awesome`,`columns`) " +
"VALUES ({0},{1},{2});", invoiceinfo.awesomeparam1, ... ,invoiceinfo.awesomeparamn);
noRowsInserted = await db.Database.ExecuteSqlCommandAsync(query);
And In my JMeter HTTP request form I entered the correct path for the above controller function. I left the parameters and files upload tabs empty while filling the body data tab with the following:
${__FileToString(/my/awesome/file/path${__eval({awesome_json_file_name_with_no_dottxt_in_end_altough_it_has_it})}.txt,,)}
Running the server and with no body data does reach the function to a breakpoint I set. So I know the route path is fine. Adding the above code to the body data causes a 415 error - Unsupported Media Type. Here is the httpsample provided by an output file by JMeter:
<httpSample t="8023" it="0" lt="8023" ct="2" ts="1544434665189" s="false" lb="HTTP Request" rc="415" rm="Unsupported Media Type" tn="Thread Group 1-3" dt="text" by="382" sby="281" ng="1" na="1">
<responseHeader class="java.lang.String">HTTP/1.1 415 Unsupported Media Type
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: todays_awesome_date
Content-Length: 103
</responseHeader>
<requestHeader class="java.lang.String">Connection: keep-alive
Content-Length: 37
Content-Type: multipart/form-data; boundary=jBtzRzXGjCNVf_3UH8LfxguE6qR9_xH
Host: awesome_ip_add
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_191)
</requestHeader>
<responseData class="java.lang.String">{"Message":"The request entity's media type 'multipart/form-data' is not supported for this resource.'}</responseData>
<cookies class="java.lang.String"></cookies>
<method class="java.lang.String">POST</method>
<queryString class="java.lang.String">--jBtzRzXGjCNVf_3UH8LfxguE6qR9_xH--
</queryString>
<java.net.URL>awesome_route_path</java.net.URL>
</httpSample>
The issue is also not with the sql syntax. It run fine from another function in the controller when parameters were given through the URL.
I also tried filling the files upload with the path and a paramter name and then in parameters to add the file and It always gives out the same error code. I just want the JSON parameters to fill the function's argument so it can be used as shown in code. What is it I'm missing? Thanks.
Add HTTP Header Manager as a child of the HTTP Request sampler
Configure it to send Content-Type header with the value of application/json
Make sure you have Use multipart/form-data for POST NOT CHECKED in the HTTP Request sampler
More information: REST API Testing - How to Do it Right
I'm trying to access an API using Postman to get a response using basic authentication, but when I submit the data, it gives me the dreaded 400 error, which apparently indicates that some headers aren't set up properly.
Here's the API info:
Request
Endpoint: {loginUrl}
Data to submit: an email and a password
POST /login HTTP/1.1
Host: {baseUrl}
Accept: application/json
Content-Type: application/json
Content-Length: 68
{
"email": "{email}",
"password": "{password}"
}
And in response, I should get an encrypted token in form of JSON, instead I'm getting this error.
Here are the postman screenshots:
Am I missing something?
I also faced the same issue and i updated my postman header with the below image. And issue solved.
From the lack of details it's difficult to offer a specific answer but I can offer something that you can try - The Request data you posted in the question looks like you should be adding:
{
"email": "{email}",
"password": "{password}"
}
In the Body section of the request but your images don't show that you've added that to the Body on any request, you've added it as a Auth header instead, so remove that before trying again. I'm not sure of the need to add the Content-Length header as that will change for different username and password combinations in the payload or for the length of the response.
In case anyone finds this helpful, I ran into the same issue and the culprit turned out to be missing headers. I knew I needed the "Content-Type": "application/json" header, which I already had in place, but I didn't know that I was missing two other headers.
The solution for me was also adding the "Content-Length" and "Host" headers in Postman.
I see some others have questioned the need for the "Content-Length" header, but in my case, the minimum three needed were "Content-Type", "Content-Length", and "Host" or it would always fail.
I faced a similar issue. it got resolved when i called my placeholder as a tuple.
I'm currently working with Angular 5.1.2 and i'm trying to get objects from http requests.
In order to verify my code, I've hardcoded a JSON response and created a Python Anywhere's web service, here's what I did :
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=UTF-8
{"Computer":[{
"ip":"192.168.0.142",
"mac":"39-D7-98-9E-5A-DC",
"name":"PC-DE-JEAN-CLAUDE"
},
{
"ip":"192.168.0.50",
"mac":"4D-49-98-30-8A-F5",
"name":"LIVEBOX-684J"
}]}
However, why my Angular app is saying that "No 'Access-Control-Allow-Origin' header is present on the requested resource" ?
Thanks
It is related to CORS issue. It happens when server and client are running on different addresses. To make it run, server need to return Access-Control-Allow-Origin as a Key:Value pair in their header response.
Access-Control-Allow-Origin: *
Specifying value as * means that the content of the address can be accessed by any other address.
It's one of the layer in securing the Internet applications.
This is a server-side problem due to CORS to prevent XSS. In order to fix, make sure your server responds with the header Access-Control-Allow-Origin: * After verifying this fixes the problem, set this header to your website URL
I am trying to run a POST call to create a new customer on Magento from another platform using "http://magentohost/api/rest/customers".
The request failed with this message:
{"messages":{"error":[{"code":400,"message":"Invalid Content-Type header"}]}}
Yet I specify well the Content-Type as follows:
Accept: application/json
Content-Type: application/json
Authorization : ...
body is something like this:
{"website_id":"1","email":"test#test.com","telephone":"","group_id":"1","prefix":"Mme","firstname":"test","lastname":"test"}
the request works fine when I fire it from chrome rest client with the same settings.
also GET method works fine.
If some one encountred the same issue please help !
I have a simple RESTLET Service which returns a JSON representation, as shown below:
#Override
#Post("json")
public Representation helloWorld() {
Representation representation = new JacksonRepresentation<Hello>(new Hello("Hello World"));
representation.setMediaType(MediaType.APPLICATION_JSON);
representation.
return representation;
}
When I query this using cURL I get the response as expected:
{"greeting":"Hello World"}
However I do not get a response when I use the browser or POSTMAN or indeed any other web REST client. I get the response 'Could not get any response' from POSTMAN.
The preview of the POSTMAN request is:
POST /testingrestlet/hello HTTP/1.1
Host: 127.0.0.1:6000
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 5141cd87-505c-e813-0045-3b7f4aa54144
I would at least have expected a REST client such as POSTMAN to work, or am I missing something?
Any help appreciated.
Regards
I think that you should use an header Accept for the content negotiation (conneg). Your request should be:
POST /testingrestlet/hello HTTP/1.1
Host: 127.0.0.1:6000
Content-Type: application/json
Accept: application/json
Cache-Control: no-cache
Postman-Token: 5141cd87-505c-e813-0045-3b7f4aa54144
As a matter of fact, since you specify the extension "json" in the annotation, Restlet expects that this header was sent with one of possible media types for JSON content. This hint is used to select the method for processing... You shouldn't have this problem if you remove the extension, as described below:
#Post
public Representation helloWorld() {
Representation representation
= new JacksonRepresentation<Hello>(new Hello("Hello World"));
representation.setMediaType(MediaType.APPLICATION_JSON);
(...)
return representation;
}
Hope it helps you,
Thierry
With help from Thierry's comments the issue was traced down to Google Chrome blocking requests to port 6000 as it considers them unsafe. The error was only visible in Chrome Debug mode where the ERR_UNSAFE_PORT error was reported. Changing to a higher port resolved the issue! Detail of this error can also be found here: https://superuser.com/questions/188006/how-to-fix-err-unsafe-port-error-on-chrome-when-browsing-to-unsafe-ports