2

When I run the below code in a python script, I'm getting the error message <Response [403]>

But when I send a get request via postman or open up the address in my browser, I'm not getting an error message and it's showing a response status of <200>

I've copied all the headers that are showing up in the network console of Google Chrome to see if perhaps the issue was tied to me not importing all the proper headers, but the issue persists even if after copying every single header.

The code that runs my API

app = FastAPI()

@app.get("/test")
def pass_parameters(parameters: Optional[str] = Query(None)):
         
    file_name = "hey.txt"
    with open(file_name, 'w') as f:
        api_token = "shdsajkhdjkasjdkasjh3242341jh" 
        parameters= {"phrase": parameters}
        print(parameters)
        response = requests.get(f"https://api.state.taxes", headers={ "Accept": "application/json", "Authorization": f"Bearer {api_token}"}, params=parameters)
        
        f.write(json.dumps(response.json(), indent=4))
        response = response.json()
   

        
    return JSONResponse(content=response, media_type="text/json")
    
if __name__ == '__main__':
        uvicorn.run(app, port=8080, host='127.0.0.1')

The code to get stuff from my API

import requests

url = "http://127.0.0.1:8080/test"

headers = {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9",
    "Connection": "keep-alive",
    "Cookie": "AMCV_1B3AA45570643167F000101%40AdobeOrg=-%7CMCIDTS%7C18944%7CMCMID%%7CMCOPTOUT-1636735667s%7CNONE%7CvVersion%7C5.1.1",
    "Host": "127.0.0.1:8080",
    "If-Modified-Since": "Sat, 19 Mar 2022 15:13:13 GMT",
    "If-None-Match": "e6ead9f56bc933ab83465",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "Windows",
    "Sec-Fetch-Dest": "document",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "none",
    "Sec-Fetch-User": "?1",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36"
}

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

print(response) --> returns <Response [403]>

But when I open my browser, the webpage opens up fine with a 200 response in the network console of the developer tools as well as within Postman. I exported the Postman call to Python and getting the same error. I saw several Stackoverflow posts were they were able to fix the issue by applying the correct header and disabling proxy in Postman, I've done both and issue persists. I'm not sure if perhaps there's a header that I'm missing?

Shane Bishop
  • 3,905
  • 4
  • 17
  • 47
SuperDummy
  • 41
  • 1
  • 7
  • What do you see if you print `response.content` and `response.text`? – Shane Bishop Mar 19 '22 at 18:15
  • Have you looked at https://stackoverflow.com/a/8287752/8593689? This answer provides details how to use requests with a proxy. – Shane Bishop Mar 19 '22 at 18:26
  • Thank you so much for sharing that with me! I'll take a look at that now! – SuperDummy Mar 19 '22 at 18:33
  • @ShaneBishop So I copied all the proxy settings from my local computer and passed it in through my API call but I'm still getting the same error. I'm confused as to why it works in my browser and in Postman, but not in visual studio code. It's working in Powershell as well. Is it because the requests package in python functions differently? I'm looking at Postman and there isn't any cookies generated. In the browser however, there's cookies that are generated and I copied/pasted those into my headers. Issue persists. – SuperDummy Mar 19 '22 at 19:53

1 Answers1

0

Making a call to localhost instead of my local IP address seemed to have fixed the issue. Like so:

http://localhost:8080

SuperDummy
  • 41
  • 1
  • 7