-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove header in middle of redirection #3490
Comments
Can you run the code with a normal, unchanged Requests, and then once the request is complete run this: for h in r.history:
print(h.headers)
print(r.headers) And then show us the output? |
Oh, sorry. Just stop setting the header yourself! You should avoid setting the Content-Type, Accept-Encoding, Content-Type, and Accept-Encoding headers in this case, as requests will handle all of those for you. |
@Lukasa That was it! I was using the data field preformatted with
Thank you for your quick and effective answer 👍 |
My pleasure! |
Hey @Lukasa, Yesterday I tested it rapidly and thought it worked. Today I'm not able to reproduce what worked... #!/usr/bin/env python
import requests
ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36'
headers = {
'User-Agent': ua,
}
s = requests.Session()
data = {
'user': 'user.name@example.com',
'pass': 'password'
}
def patch():
import http.client as http_client
old_send = http_client.HTTPConnection.send
def new_send( self, data ):
print('=== SEND CALL NB {}'.format(new_send.i))
print(data.decode('utf-8'))
if new_send.i == 2:
data = '\n'.join([l for l in data.decode('utf-8').split('\n') if not l.startswith('Content-Type:')]).encode('utf-8')
print('=== ALTERED DATA')
print(data.decode('utf-8'))
print('=== END')
new_send.i += 1
return old_send(self, data)
new_send.i = 0
http_client.HTTPConnection.send = new_send
patch()
r = s.post('https://www.example.com/openam/UI/Login?realm=front_office&service=EEService&goto=https://www.example.com/ice/rest/aiguillagemp/redirect?dest=',
headers=headers, data=data) The output with the patch commented is:
You can see that the header The log if I activate the workaround suppressing the header for the specific request:
Sorry for the french in error messages/URLs. I, unfortunately, cannot reveal what is the website responsible for the misbehaviour (NDA). Am I doing something wrong here? |
Hrm, no. It does look like we don't strip the content-type header when we remove the body. We should probably do that. This is a bug. |
For any contributor who wants to submit a patch for this, look at |
#3490 removing Content-Type and Transfer-Encoding headers on redirect
This was fixed in #3493 |
Hey there,
I'm having a pretty specific issue while trying to reproduce the login sequence of a heavy professional website (lots of redirect / cookies involved).
The login sequence was tested working on Chrome.
I spent some time debugging and finally tracked down the issue, that is that the server will not accept the
Content-Type
header that was required on the first request on the first redirect, thus making the second request crash with a500
.First request shown in Chrome's dev inspector:
Second request (first redirect) show in Chrome's dev inspector:
Notice of how on the second request, no
Content-Type
header. The presence of aContent-Type
header would make the request fail.The code (URLs are mangled):
I worked it around by removing the
Content-Type
header at runtime on the 3rd send (first is the first request header, 2nd is the first request data) to confirm that it was the issue (it was!)Patch code added before the request.
Any idea of how to solve this one?
Note: I have no control at all of the remote server.
Note: I intend to make a lot of requests while being logged in, so the I would like to stay with a session.
The text was updated successfully, but these errors were encountered: