I am a python newbie and i have a controler that get Post requests.
I try to print to log file the request that it receive, i am able to print the body but how can i extract all the request include the headers?
I am using request.POST.get() to get the body/data from the request.
Thanks
To get request headers, you can use request.META, as it returns dictionary containing all available HTTP headers.
headers = request.META
request.POST should give you the POST body if it is get use request.GET
if the request body is json use request.data
Related
I try to read JSON-formatted data from the following public URL: http://ws-old.parlament.ch/factions?format=json. Unfortunately, I was not able to convert the response to JSON as I always get the HTML-formatted content back from my request. Somehow the request seems to completely ignore the parameters for JSON formatting passed with the URL:
import urllib.request
response = urllib.request.urlopen('http://ws-old.parlament.ch/factions?format=json')
response_text = response.read()
print(response_text) #why is this HTML?
Does somebody know how I am able to get the JSON formatted content as displayed in the web browser?
You need to add "Accept": "text/json" to request header.
For example using requests package:
r = requests.get(r'http://ws-old.parlament.ch/factions?format=json',
headers={'Accept':'text/json'})
print(r.json())
Result:
[{'id': 3, 'updated': '2022-02-22T14:59:17Z', 'abbreviation': ...
Sorry for you but these web services have a misleading implementation. The format query parameter is useless. As pointed out by #maciek97x only the header Accept: <format> will be considered for the formatting.
So your can directly call the endpoint without the ?format=json but with the header Accept: text/json.
I am using json and urllib to pull data from the IBKR client portal API:
data = json.load(urlopen("https://localhost:5000/v1/api/portfolio/{acct ID}/positions"
I am having trouble understanding how to pass information to it, for example, to place orders. The documentation just says "pass json here" basically and gives the url: /server/account/{accountId}/orders.
There are no other instructions in the documents.
I would imagine the API is expecting a POST request containing a JSON object in the body of the request.
Using the requests library (pip install requests)...
r = requests.post('.....server/account/{accountId}/orders', json={"key": "value"})
r is the response from that POST request.
You can then do these to view the response from the server and figure out if you are posting valid data:
r.status_code
r.json()
I'm sending a POST request, with python-requests in Python 3.5, using:
r = requests.post(apiEndpoint, data=jsonPayload, headers=headersToMergeIn)
I can inspect the headers and body after sending the request like this:
print(r.request.headers)
print(r.request.body)
Is there any way to inspect the full request headers (not just the ones i'm merging in) and body before sending the request?
Note: I need this for an api which requires me to build a hash off of a subset of the headers, and another hash off the full body.
You probably want Prepared Requests
Using python requests to talk to the github api.
response = requests.post('https://api.github.com/orgs/orgname/repos', auth, data)
If the request returns a 405 error I get HTML in the response.text
<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
</body>
</html>
If the request returns a 422 error I get JSON in the response.text
{"message":"Validation Failed","errors": [{"resource":"Repository","code":"custom","field":"name","message":"name already exists on this account"}],"documentation_url":"https://developer.github.com/v3/repos/#create"}
Can I force the API to only return JSON?
If not, can I find out what type the response content will be?
In response to:
If not, can I find out what type the response content will be?
If you want to know why github returns html rather than JSON for the API call, I can not answer that; however, to the answer to the above asked question, you can look at the "content" header of the http response.
response = requests.post('https://api.github.com/orgs/orgname/repos', auth, data)
if "application/json" in response.headers['content-type']:
print response.json()
In response to:
Can I force the API to only return JSON?
you could try forcing the "Accept header within the request; however, this is down to GitHub server if it wants to serve json in the response
response = requests.post('https://api.github.com/orgs/omnifone/repos', auth=auth, data=data, headers={"Accept": "application/json"})
While using requests to download a webpage, we store the result of that operation in a response object. What I could not understand is, exactly what is stored in the response object? Is it the source code of that page in HTML or is it the entire string on the page that is stored?
It is an instance of the lower level Response class of the python requests library. The literal description from the documentation is..
The Response object, which contains a server's response to an HTTP request.
Every HTTP request sent returns a response from the server (the Response object) which includes quite a bit of information.
You can find all the info you need here, and also here is the github link.
Server and Client use HTTP Protocol to send/receive information.
response stores all information from server - HTTP headers (for example: cookies, status code) and HTTP body (mostly HTML but it can be JSON or file or other)
wikipedia: HTTP Protocol
BTW: request stores HTTP headers and HTTP body too. (sometimes HTTP body can be empty)