session auth in python

后端 未结 1 1694
滥情空心
滥情空心 2021-01-22 13:45

Using session from requests module in python, it seems that the session sends authorization only with first request, I can\'t understand why this happened.

相关标签:
1条回答
  • 2021-01-22 14:04

    What I see when I run that exact code in your comment is that the Authorization header is missing in the first print, yet it is present in the second. This seems to be the opposite of the problem that you report.

    This is explained by the fact that the first request is redirected by a 301 response, and the auth header is not propagated in the follow up request to the redirected location. You can see that the auth header was sent in the initial request by looking in response.history[0].request.headers.

    The second request is not redirected because the session has kept the connection to the host open (due the the Connection: keep-alive header), so the auth headers appear when you print response.request.headers.

    I doubt that you are actually using https://test.com, but probably a similar thing is happening with the server that you are using.

    For testing I recommend using the very handy public test HTTP server https://httpbin.org/headers. This will return the headers received by the server in the response body. You can test redirected requests with one of the redirect URLs.

    0 讨论(0)
提交回复
热议问题