Skip to content
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

Running prepare_headers on PreparedRequest break it #4833

Open
mlubimow opened this issue Oct 17, 2018 · 5 comments
Open

Running prepare_headers on PreparedRequest break it #4833

mlubimow opened this issue Oct 17, 2018 · 5 comments

Comments

@mlubimow
Copy link

After calling prepare() on a request, PreparedRequest object is returned. If its a PATCH request (GET is working as expected), and prepare_headers is called, it's not possible to send this request.

Expected Result

Headers should be added to a prepared request and it should be possible to send it later.

Actual Result

An exception is thrown:

Traceback (most recent call last):
  File "r.py", line 7, in <module>
    s.send(r)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 466, in send
    low_conn.send(hex(len(i))[2:].encode('utf-8'))
TypeError: object of type 'int' has no len()

Reproduction Steps

import requests

r = requests.Request('PATCH', 'http://domain.com/objects/32', json={'name': 'new name'})
r = r.prepare()

s = requests.Session()
r.prepare_headers({'Authorization': 'Token 123'})
s.send(r)

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.7"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.6.6"
  },
  "platform": {
    "release": "4.9.87-linuxkit-aufs",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.19.1"
  },
  "system_ssl": {
    "version": "1010006f"
  },
  "urllib3": {
    "version": "1.23"
  },
  "using_pyopenssl": false
}
@erayerdin
Copy link

erayerdin commented Oct 27, 2018

The error occurs while HTTPAdapter::send method tries to read the length of request.body.

@sigmavirus24
Copy link
Contributor

@mlubinmow what do you think should be happening here? I'm not convinced this is a bug in Requests but rather in using undocumented methods.

Specifically http://docs.python-requests.org/en/master/user/advanced/?highlight=PreparedRequest#prepared-requests tells you how you're expected to interact with PreparedRequests and makes no guarantees about the prepare_* methods on the object.

@mlubimow
Copy link
Author

mlubimow commented Oct 29, 2018

@sigmavirus24 I disagree, function is listed in Devs interface and is inconsistent, as it works for GET request. I would expect it to.. just work ;) As mentioned in an issue.

http://docs.python-requests.org/en/master/api/#lower-lower-level-classes

@sigmavirus24
Copy link
Contributor

@mlubimow just because something is included in auto-generated documentation doesn't mean it just magically works as you expect it to.

@sigmavirus24
Copy link
Contributor

To be clearer, the documentation I linked to explains how to actually work with the prepared request flow. If something happens to work in one case when you do something the system wasn't designed for, it doesn't mean it must work in a different case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants