Mari
Mari

Reputation: 11

How can I fix error 403 on python rest api?

I don't have much experience with API's. I'm currently working on a Python program using the following api: https://uk.api.just-eat.io/docs#section/Just-Eat-API.

I'm focusing on retrieving json data by postal code: https://uk.api.just-eat.io/docs#operation/SearchByPostcode

import requests
import json


response = requests.get("https://uk.api.just-eat.io/restaurants/bypostcode/NW32YT")
print(response.json())
print(response.status_code)

When i execute the above I receive the following message:

403

Traceback (most recent call last):
  File "/Users/mari/python/./api.py", line 7, in <module>
    print(response.json())
  File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/local/Cellar/[email protected]/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/[email protected]/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/[email protected]/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I was having issues getting the json data but yesterday evening, the status code was 200 and I was able to see the json data from my terminal. Am i missing something in my code? or is the the api unstable?

-Many thanks in advance.

Upvotes: 0

Views: 1787

Answers (1)

Jem Tucker
Jem Tucker

Reputation: 1163

You need to add an Authorization header as shown in the API documentation. If you do not have an API Key you will need to request one from them.

PUT https://uk-partnerapi.just-eat.io/orders/abcd1234 HTTP/1.1
Authorization: JE-API-KEY abcd123456789

To do this with the requests library you can change your request to

requests.get("https://uk.api.just-eat.io/restaurants/bypostcode/NW32YT", headers={'Authorization': 'JE-API-KEY <MY_TOKEN>'})

Upvotes: 1

Related Questions