Request failed in Django but succeeds when I run script standalone

I have a script to get Steam inventory items and I want to use it in Django but when I use it in view or even I want to call the function with custom command in Django like this: python manage.py test_inventory I get 429 status code from Steam.
But when I run this script standalone and not in Django, it will be okay with status code 200 and it will return the items.

My function:

import requests

r = requests.get(
    "https://steamcommunity.com/inventory/76561198841800602/730/2/",
    params={
        "l": "english",
        "count": 2000,
    },
    timeout=30,
)

print(r.status_code)

Function is the same but I have error in Django.

This has nothing to do with Django because the script still returns 429 when run outside Django. You might want to clarify how it works for you outside Django.

You have a missing header as @Amit Tiwari pointed out in the comments. I added that header and got a 200 status code response.

So add headers = {"Accept-Language": "en-US,en;q=0.9",}

import requests

r = requests.get(
"https://steamcommunity.com/inventory/76561198841800602/730/2/", params={"l": "english","count": 2000,}, headers={"Accept-Language": "en-US,en;q=0.9",}, timeout=30,)
print(r.status_code) # 200
print(r.text) # response not null

You should add the user-agent header to your request :

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.>
}

r = requests.get(
    "https://steamcommunity.com/inventory/76561198841800602/730/2/",
    params={
        "l": "english",
        "count": 2000,
    },
    headers=headers,
    timeout=30,
)

print(r.status_code)

This shold fix the issue.
but you should also take in mind the rate limiting steam may have.

Вернуться на верх