How to add header in requests - python

Is there any other elegant way to add header to requests :
import requests
requests.get(url,headers={'Authorization', 'GoogleLogin auth=%s' % authorization_token})
doesn't work, while urllib2 worked :
import urllib2
request = urllib2.Request('http://maps.google.com/maps/feeds/maps/default/full')
request.add_header('Authorization', 'GoogleLogin auth=%s' % authorization_token)
urllib2.urlopen(request).read()

You can add headers by passing a dictionary as an argument.
This should work:
requests.get(url,headers={'Authorization': 'GoogleLogin auth=%s' % authorization_token})
Why your code not worked?
You were not passing a dictionary to the headers argument. You were passing values according to the format defined in add_header() function.
According to docs,
requests.get(url, params=None, headers=None, cookies=None, auth=None,
timeout=None)
headers – (optional) Dictionary of HTTP Headers to send with the
Request.
Why request.add_header() worked?
Your way of adding headers using request.add_header()worked because the function is defined as such in the urllib2 module.
Request.add_header(key, val)
It accepts two arguments -
Header name (key of dict defined earlier)
Header value(value of the corresponding key in the dict defined earlier)

You can pass dictionary through headers keyword. This is very elegant in Python :-)
headers = {
"header_name": "header_value",
}
requests.get(url, headers=headers)

You can add custom headers to a requests request using the following format which uses a Python dictionary having a colon, :, in its syntax.
r = requests.get(url, headers={'Authorization': 'GoogleLogin auth=%s' % authorization_token})
This is presented in the Requests documentation for custom headers as follows:
>>> url = 'https://api.github.com/some/endpoint'
>>> headers = {'user-agent': 'my-app/0.0.1'}
>>> r = requests.get(url, headers=headers)

Headers should be a dict, thus this should work
headers= {}
headers['Authorization']= 'GoogleLogin auth=%s' % authorization_token
requests.get(url, headers=headers)

Related

requests returning "The types 'Edm.Boolean' and 'Edm.String' are not compatible."

I'm having a issue when making a python GET to a API using requests lib and a payload, the error returned is a 400 "The types 'Edm.Boolean' and 'Edm.String' are not compatible.", see the code below.
import requests
url = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais"
querystring = {"$format":"json","$top":"1","$filter":'DataReferencia eq 06/2022 and Indicador eq Câmbio'}
headers = {'Cookie': 'TS01d9825e=0198c2d644d1afbffdff044aa68d6a5088fc806b2e1ac62e24367e61bb3c10a8c2b7eec94d00069c6d44269e2653616599c149f905; dtCookie=944AB33FFF88A610C23EBA9EBA852F9B|ZXhwZWN0YXRpdmFzfDE; BIGipServer~was-p_as3~was-p~pool_was-p_default_443=1020268972.47873.0000; JSESSIONID=00000xsbngFt_CzrLQkgb15FC3A:1cn7m3fq4; TS013694c2=0198c2d6446fb34209160d9d557965883a3a94328644e3b0278c6e4b3db9ee1bfc5903a5df8e1f1db029ba9f5d4d775874538a8854'}
response = requests.request("GET", url, headers=headers, params=querystring)
The headers I've extracted direct from the Postman tool!
When I use print(response.url) the returned URL is the following (note the + sign formatting)
https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?%24format=json&%24top=1&%24filter=DataReferencia+eq+06%2F2022+and+Indicador+eq+C%C3%A2mbio
When I do not use the payload format and use the whole URL (like extracted from postman) it works, see below.
import requests
url = "https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?$format=json&$top=1&$filter=DataReferencia eq '06/2022' and Indicador eq 'Câmbio'"
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
But the URL in this way come like this,
https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?$format=json&$top=1&$filter=DataReferencia%20eq%20'06/2022'%20and%20Indicador%20%20eq%20'C%C3%A2mbio'
Instead of + signs composing the URL it's encoded with %20, so I'm wondering it's a URL encoding problem, but I can't get it right!

Loading JSON using Python Requests wrong type

I'm trying to load some data to ML using Python. This works ok but the type is set to 'T' and not to 'J' within ML.
I'd like to resolve this. The header setting seems to be just for show so how do I do this?
# Sending data
data = {'meting': '477', 'bericht': '473', 'plant': '01'}
url = 'http://server:8000/v1/documents?database=thijsPlantjes&extension=json'
headers = {'Content-Type': 'application/json'}
r = requests.post(url, json = json.dumps(data), auth=HTTPDigestAuth('plantje', 'password'), headers = headers)
If you use json parameter, requests will serialize for you, so you don't need to json.dumps yourself.
And it will also set content-type for you; you can drop headers keyword argument.
r = requests.post(url, json=data, auth=HTTPDigestAuth('plantje', 'password'))
According to requests documentation:
Instead of encoding the dict yourself, you can also pass it directly
using the json parameter (added in version 2.4.2) and it will be
encoded automatically:

Posting a json data in url -- Python

I have JSON data stored in a variable in [{"totalspend": 3240.650785131, "dailybudget": 50.0}] format.
I am trying to post this JSON data to a url using:
import requests
r = requests.post("myurl", myjson)
but I am not able to see the result on my url after executing the code.
Your server most likely expects the Content-Type: application/json header to be set:
r = requests.post("myurl", data=myjson,
headers={'Content-Type': 'application/json'})
Do make sure that myjson is an actual JSON string and not a Python list.
If you are using requests version 2.4.2 or newer, you can leave the encoding of the JSON data entirely to the library; it'll set the correct Content-Type header for you automatically. You'd pass in the Python object (not a JSON string) to the json keyword argument:
r = requests.post("myurl", data=myobject)
You need to set headers first :
Try :
import json
import requests
payload = {"totalspend": 3240.650785131, "dailybudget": 50.0}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)

BLS API data using cURL and requests library?

All, I'm trying to implement a curl request to get data from the BLS. Following their example here (they show the curl request), my code looks like this:
import requests
headers = {'Content-type': 'application/json'}
params = {"seriesid":["LEU0254555900", "APU0000701111"],"startyear":"2002", "endyear":"2012"}
p = requests.post('http://api.bls.gov/publicAPI/v1/timeseries/data/', params = params,headers = headers)
print p.url
print p.content
I'm getting the following (error) output:
http://api.bls.gov/publicAPI/v1/timeseries/data/?seriesid=LEU0254555900&seriesid=APU0000701111&endyear=2012&startyear=2002
{"status":"REQUEST_FAILED","responseTime":0,"message":["Sorry, an
internal error occurred. Please check your input parameters and try
your request again."],"Results":null}
Anyone had to deal with the BLS api and python?
Is the requests library the best for this?
You need to send the data as json, not pass it as a params dict. params sets the url parameters, which is not what you want, you need to pass it as data.
This should work:
import requests
import json
headers = {'Content-type': 'application/json'}
data = json.dumps({"seriesid":["LEU0254555900", "APU0000701111"],"startyear":"2002", "endyear":"2012"})
p = requests.post('http://api.bls.gov/publicAPI/v1/timeseries/data/', data=data, headers=headers)
print p.url
print p.content

Problem making a GET request and spoof User-Agent in urllib2

With this code, urllib2 make a GET request:
#!/usr/bin/python
import urllib2
req = urllib2.Request('http://www.google.fr')
req.add_header('User-Agent', '')
response = urllib2.urlopen(req)
With this one (which is almost the same), a POST request:
#!/usr/bin/python
import urllib2
headers = { 'User-Agent' : '' }
req = urllib2.Request('http://www.google.fr', '', headers)
response = urllib2.urlopen(req)
My question is: how can i make a GET request with the second code style ?
The documentation (http://docs.python.org/release/2.6.5/library/urllib2.html) says that
headers should be a dictionary, and
will be treated as if add_header() was
called with each key and value as
arguments
Yeah, except that in order to use the headers parameter, you have to pass data, and when data is passed, the request become a POST.
Any help will be very appreciated.
Use:
req = urllib2.Request('http://www.google.fr', None, headers)
or:
req = urllib2.Request('http://www.google.fr', headers=headers)

Categories