I am using requests to POST my data to a URL. I need to know what is the header that is sending by my request?
import requests
conf_json = {"key_1": "value_1", "key_2": "value_2"}
r = requests.post(API-URL, json=conf_json)
I can get response header by r.headers
, but I need request header and I don't know how I should get it. can you please guide me?
HTTP headers let the client and the server pass additional information with an HTTP request or response. All the headers are case-insensitive, headers fields are separated by colon, key-value pairs in clear-text string format.
A request header is an HTTP header that can be used in an HTTP request to provide information about the request context, so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.
In order to pass HTTP headers into a POST request using the Python requests library, you can use the headers= parameter in the . post() function. The headers= parameter accepts a Python dictionary of key-value pairs, where the key represents the header type and the value is the header value.
By default, requests do not have a timeout unless you explicitly specify one. It is recommended to set a timeout for nearly all requests; otherwise, your code may freeze, and your program will become non-responsive.
import requests
conf_json = {"key_1": "value_1", "key_2": "value_2"}
r = requests.post(API_URL, json=conf_json)
# get request headers
print(r.request.headers)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With