I am using python requests library to get the data from adzuna api .
if i try
JavaScript
x
5
1
r = requests.get("http://api.adzuna.com/v1/api/jobs/gb/search/1?app_id=appID
2
&app_key=appKEY&results_per_page=50&
3
what=entry%20level&content-type=application/json")
4
print r.text
5
it is fetching me data But if i wrap this inside a fuction
JavaScript
1
8
1
def getData ():
2
r = requests.get("http://api.adzuna.com/v1/api/jobs/gb/search/1?app_id=appID
3
&app_key=appKey&results_per_page=50&
4
what=entry%20level&content-type=application/json")
5
print r.text
6
7
getData()
8
it is giving me following exception
JavaScript
1
2
1
{"display":"Authorisation failed","__CLASS__":"Adzuna::API::Response::Exception","doc":"http://api.adzuna.com/v1/doc","exception":"AUTH_FAIL"}
2
what is the wrong with this function ?
Advertisement
Answer
(edited) Since using a single-line query fixed your problem, I’ll recommend that you don’t try to build the query yourself but instead let requests do it for you, like so:
JavaScript
1
7
1
query = {'app_id': 'appID',
2
'app_key': 'appKey',
3
'content-type': 'application/json',
4
'results_per_page': '50',
5
'what': 'entry level'}
6
r = requests.get('http://api.adzuna.com/v1/api/jobs/gb/search/1', params=query)
7
This should be less error-prone and is certainly more convenient.