python requests, I got different response status than postman's result.

Viewed 800

requests: 2.19.1

python: 3.6

url(It's just url, There's no special meaning): http://moneycake.tistory.com/43

Python code(actually, pycharm django code):

try:
    r = requests.head('http://moneycake.tistory.com/43', allow_redirects=False)
    print(r.status_code)
except requests.exceptions.RequestException as e:
    pass

It returns >>> 403

But when I do this on Postman with method GET and default settings,

It returns 200 OK.

Why this happen?

And How can I get 200 OK status response on pycharm?

1 Answers

You need to set the User-Agent:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
}

try:
    r = requests.head('http://moneycake.tistory.com/43', allow_redirects=False, headers=headers)
    print(r.status_code)
except requests.exceptions.RequestException as e:
    pass

Output

200
Related