I am using Python 2.7.5, Django 1.7, requests 2.4.1, and doing some simple testing. However, it seems like when I call requests.post, the method is doing a GET instead.
Thanks to Martijn for some debugging tips! The problem was the RESTful API was redirecting me from http:// to https://, which caused the library to return the "second" request (GET)...
To be clear, whenever requests receives a redirect (with a certain status code) we have to perform certain transformations on the request.
In cases like this, when you see something very unexpected the best debugging tips are to retry your request but with allow_redirects=False
. This will immediately return the 30x response. Alternatively, you can also check r.history
to see if there are any 30x responses that were followed. In this case you probably would have seen something like
>>> r.request.method
'GET'
>>> r.history
[<Response [302]>,]
>>> r.history[0].request.method
'POST'
We know that doing this can cause unexpected behaviour for users (as it just did to you) but it's the only correct way to operate on the web.
I hope this helps you understand why this happened beyond the fact that it was a redirect, and hopefully it gives you and others tools to debug this in the future.