mochamethod
mochamethod

Reputation: 107

How to set up a request header for an Authenticated API GET request

I'm trying to make an authenticated GET request to an API. This is one of my first attempts working with Python's request library. I've looked over similar posts to this one, but they're a bit too generic to answer my question, it seems. Their answers work for nearly every other case I've worked with, so it feel a bit stuck.

The request header is fairly lengthy:

':authority': 'api-WEBSITE.com',
':method': 'GET',
':path': 'ENDPOINT',
':scheme': 'https',
'accept': 'application/json',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'authorization': 'AUTH_TOKEN', 
'content-type': 'application/json',
'cookie': 'VERY_LONG_COOKIE',
'origin': 'https://WEBSITE.com',
'referer': 'https://WEBSITE.com/app',
'user-agent': 'LIST_OF_BROWSERS'

My code that makes this request:

import requests

requestURL = "https://api-WEBSITE.com/ENDPOINT"

parameters = {
    ':authority': 'api-WEBSITE.com',
    ':method': 'GET',
    ':path': 'ENDPOINT',
    ':scheme': 'https',
    'accept': 'application/json',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9',
    'authorization': 'AUTH_TOKEN', 
    'content-type': 'application/json',
    'cookie': 'VERY_LONG_COOKIE',
    'origin': 'https://WEBSITE.com',
    'referer': 'https://WEBSITE.com/app',
    'user-agent': 'LIST_OF_BROWSERS'
}

response = requests.get(requestURL, parameters)

print(response.status_code)

When I run this, I'm getting a 401 status code asking for authentication; however, I can't seem to find out what's throwing this 401 error.

Upvotes: 0

Views: 84

Answers (1)

bad
bad

Reputation: 56

To supply headers for a python request: you must do this r = requests.get(url, headers = headersDict)

Where headersDict is a valid dictionary of the headers you want added to the request

Upvotes: 1

Related Questions