Home > Mobile >  How to call request with api key in pyton
How to call request with api key in pyton

Time:11-23

I have api like this : [api_postman]

1

I want to call this api in python, this is my code :

def get_province():
headers = {
        'Content-type': 'application/json',
        'x-api-key': api_key
    }

response = requests.get(url, headers=headers)

return response.json()

But, i've got

error 500 : Internal Server Error.

I think there's something wrong with the header. Can anyone help me?

CodePudding user response:

Judging from screenshot, it should be Authorization not x-api-key i.e.

headers = {
        'Content-type': 'application/json',
        'Authorization': api_key
    }

Please test that and write what is result

CodePudding user response:

You can provide an API key like this:

from requests.auth import HTTPBasicAuth
import requests

# other code you wrote
auth = HTTPBasicAuth('apikey', 'ILove99Dollars')

response = requests.get(url, headers=headers, auth=auth)
  • Related