python requests XHR - get response 405

3 weeks ago 16
ARTICLE AD BOX

A 405 Method Not Allowed means the endpoint explicitly rejects the HTTP method you’re using. In this case, the issue is not Python requests, it’s the server.

Even if the request is marked as “XHR” in the browser, that does not imply it supports GET. Many XHR endpoints only accept POST (or another method) and will return 405 for GET, regardless of headers like X-Requested-With.

Things to check in DevTools → Network for the working browser request:

1. HTTP method

If the XHR uses POST, you must use requests.post(), not get().

2. Request body / parameters

The browser request may include a JSON body or required query parameters. Sending an empty GET will fail.

3. Required headers

Often needed:

User-Agent

Accept

Referer

Sometimes auth or CSRF headers

Headers alone will not fix a wrong HTTP method.

Example if the XHR is actually POST:

import requests

url = "..."

headers = {

"User-Agent": "Mozilla/5.0", "Accept": "application/json", "X-Requested-With": "XMLHttpRequest", "Referer": "..."

}

payload = {...} # match the browser request body

r = requests.post(url, headers=headers, json=payload)

print(r.status_code, r.text)

Bottom line:

A 405 response means the server does not allow GET on that endpoint. Match the method, body, and headers exactly to what the browser sends.

Read Entire Article