Skip to content
Advertisement

Requests.get failing when using inside function

I am using python requests library to get the data from adzuna api .

if i try

r = requests.get("http://api.adzuna.com/v1/api/jobs/gb/search/1?app_id=appID
    &app_key=appKEY&results_per_page=50&
    what=entry%20level&content-type=application/json")
print r.text

it is fetching me data But if i wrap this inside a fuction

def getData ():
    r = requests.get("http://api.adzuna.com/v1/api/jobs/gb/search/1?app_id=appID
    &app_key=appKey&results_per_page=50&
    what=entry%20level&content-type=application/json")
    print r.text

getData()

it is giving me following exception

{"display":"Authorisation failed","__CLASS__":"Adzuna::API::Response::Exception","doc":"http://api.adzuna.com/v1/doc","exception":"AUTH_FAIL"}

what is the wrong with this function ?

See the Image for more info

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:

query = {'app_id': 'appID',
         'app_key': 'appKey',
         'content-type': 'application/json',
         'results_per_page': '50',
         'what': 'entry level'}
r = requests.get('http://api.adzuna.com/v1/api/jobs/gb/search/1', params=query)

This should be less error-prone and is certainly more convenient.

Advertisement