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

requests.post should accept strings in addition to dicts #48

Closed
pydanny opened this issue May 27, 2011 · 2 comments
Closed

requests.post should accept strings in addition to dicts #48

pydanny opened this issue May 27, 2011 · 2 comments

Comments

@pydanny
Copy link

pydanny commented May 27, 2011

The specification for HTTP POST is that it has a "message body". HTML forms sticks encoded key/values representing form inputs into that message body when submitted. However, this means that currently requests.post can't accept JSON or XML message bodies, which could make it hard to use with some APIs. Therefore, I propose the following:

"""inside of requests.post method"""

if isinstance(data, dict):
    # process as existing code now handles POST
elif isinstance(data, str) or isinstance(data, unicode):
    # Stick into message body
    # Do encoding if that is what the spec demands (I'm not sure)
else:
    # raise InvalidRequestDataFormat

"""Example of implementation"""

import json

import requests

post_dict = {"hello":"world"}
r = request.post(form_url, post_dict)

post_json = json.dumps(post_dict)
r = request.post(json_url, post_json)
@kennethreitz
Copy link
Contributor

Request.post accepts both dicts and bytestrings for the data parameter already :)

@kennethreitz
Copy link
Contributor

Thanks for submitting this issue though. Clearly, the docs need to reflect that capability. :)

@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