1

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

anilkunchalaece
  • 388
  • 2
  • 10

3 Answers3

0

In the second url you have a sapce between gb/search/ and the 1 after him.

ddor254
  • 1,570
  • 1
  • 12
  • 28
  • thanks for noticing (edited question)- that's a another copy paste error. the url is working but authorization is failing i.e getting an AUTH_FAIL Exception – anilkunchalaece Jan 11 '18 at 08:51
0

(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.

Nathan Vērzemnieks
  • 5,495
  • 1
  • 11
  • 23
0

I faced the same problem with a POST call and learned that problem was with formatting the text I was passing, some APIs can't handle any change on the format. inside function you have extra indentation and that messes up what I was posting. Instead of passing simple string:

  • Pass parameters in a json format if possible
  • Have all in one line
  • Reformat and print the string and make sure they look exactly the same in and out of function.
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ellie
  • 1
  • 1