I'm using Python's requests module to make a POST request to a certain website. My goal is to get the response as JSON.
The issue: the Content-Type header of the response is text/html. Trying to parse it as JSON raises an error. Printing the content of the response gives me a bunch of convoluted html which I don't want.
However, on Chrome Dev Tools > Network, I can see the same POST request's response as a JSON object. Also, the Content-Type header of the response on Dev Tools clearly says application/json.
My goal is to force the Content-Type of the response to application/json in my Python script.
I tried the following:
Set the header manually:
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.json()
No luck.
Set Content-Type and Accept to application/json in the request. This didn't make much sense but it was worth the try.
Send my request data as multipart/form-data as it says in Dev Tools. No luck.
This is almost exactly the same issue as this question: Response content-type text/html while expected application/json, but that one was in a more specific context and the solution doesn't apply to me.
Thank you!
I have same issue as your After 10 hour research I solve my problem by using below code
import requests
import json
url = ""
headers = {"content-type": "application/json; charset=UTF-8"}
payload = {}
r = requests.post(url, headers=headers, data=json.dumps(payload))
Hope, This will Help you
I am using Home Assistant, which templates data via Jinja2.
Is it possible to use a Jinja2 template to remove the HTTP POST header and keep the JSON data only?
The data I am using is:
POST /api/V1/sensor/UploadSensor HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: al.lewei50.com
Connection:close
Content-Length: 448
{"method":"uploadsn","version":"ABC","type":"AL_TH","SN":"ABC","Data":[13.8,8.4,352.8,364.4,10.0,257.7,7616,50,25.700,29030.140,null,4868,3060,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,2575,2540,2501,10.0,9.9,253.6,251.4,50.03,50.04,50.04],"Information":["null","000.01","ABC ","ABC"],"Status":"4883"}
and I need to have a template to show only:
{"method":"uploadsn","version":"ABC","type":"AL_TH","SN":"ABC","Data":[13.8,8.4,352.8,364.4,10.0,257.7,7616,50,25.700,29030.140,null,4868,3060,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,2575,2540,2501,10.0,9.9,253.6,251.4,50.03,50.04,50.04],"Information":["null","000.01","ABC ","ABC"],"Status":"4883"}
An HTTP response consists of the header, a blank line, and then the response body. Since you want only the response body, your solution is:
Split your data on the empty line, and then
Take the second element of the resulting list.
Assuming your data is in a variable named response, you want something like:
{{ response.split('\n\n')[1] }}
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.
I'm having problems to convert an XML response file to JSON using XSJS, Any help will be appreciated. Thanks!
It is actually pretty simple. While sending the request using XSJS to the server...your URL path should contain the format specifier.
http://myhost:8000/exampleapp/resource.xsjs/some/more/options?$format=JSON
or if you do not want to append to the URL then include the following line your request header
accept: application/json
I want to send JSON data (stringified) in an ajax POST request. How should I think about using application/json versus using application/x-www-form-urlencoded (along with encodeURIComponent)?
application/json is a media type (type of content). application/x-www-form-urlencoded is a form encode type. It is meaningless to compare them. You are posting data to a server so you need to use enctype is application/x-www-form-urlencoded. You can encode the entire JSON string using encodeURIComponent function and send this encoded JSON via a form parameter.