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

Accept file-like objects as request data #292

Closed
zacharyvoase opened this issue Nov 28, 2011 · 2 comments
Closed

Accept file-like objects as request data #292

zacharyvoase opened this issue Nov 28, 2011 · 2 comments

Comments

@zacharyvoase
Copy link

I'd like it to be possible to pass in a file-like object as the source of request data, provided Content-Length and Content-Type headers have been specified already. Here's an example of what I'd like to do:

response = requests.put(
    'http://example.com/endpoint',
    data=open('filename', 'rb'),
    headers={
        "Content-Type": "text/plain",
        "Content-Length": os.stat('filename').st_size})

Alternatively, requests could even recognise that I've passed in a file object and perform an os.fstat(file_like_obj.fileno()) automatically.

Here's the bit of code that seems to be breaking:

https://github.com/kennethreitz/requests/blob/c7b99f78192bdff62fad4f92039396c0dfbe4fb8/requests/models.py#L267

When I pass in an object that is neither a string nor a dictionary, the call to dict() fails.

I'm able and willing to work on this in a fork, I just figured it would be better to submit it as an issue first.

@kennethreitz
Copy link
Contributor

Interesting. I see no reason why we couldn't support that.

Supporting this everywhere would also force us to revisit the files parameter. Currently, it expects file-like objects. It should also accept bytes/unicode strings.

@zacharyvoase
Copy link
Author

In this case, I'd imagine that the files parameter wouldn't even be used in Request.send(); by that point, the data could be put into one large file-like object with a computed Content-Length.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants