Python Requests POST doing a GET?

前端 未结 2 586
南方客
南方客 2021-01-04 01:28

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.

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 01:58

    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
    [,]
    >>> 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.

提交回复
热议问题