-
-
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
Add json parameter #2258
Add json parameter #2258
Changes from all commits
8f17741
b34a496
402f3b4
0713e09
8ed941f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,8 @@ | |
CONTENT_CHUNK_SIZE = 10 * 1024 | ||
ITER_CHUNK_SIZE = 512 | ||
|
||
json_dumps = json.dumps | ||
|
||
|
||
class RequestEncodingMixin(object): | ||
@property | ||
|
@@ -189,7 +191,8 @@ class Request(RequestHooksMixin): | |
:param url: URL to send. | ||
:param headers: dictionary of headers to send. | ||
:param files: dictionary of {filename: fileobject} files to multipart upload. | ||
:param data: the body to attach the request. If a dictionary is provided, form-encoding will take place. | ||
:param data: the body to attach to the request. If a dictionary is provided, form-encoding will take place. | ||
:param json: json for the body to attach to the request (if data is not specified). | ||
:param params: dictionary of URL parameters to append to the URL. | ||
:param auth: Auth handler or (user, pass) tuple. | ||
:param cookies: dictionary or CookieJar of cookies to attach to this request. | ||
|
@@ -209,6 +212,7 @@ def __init__(self, | |
headers=None, | ||
files=None, | ||
data=None, | ||
json=None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I'm late to the party, was out - wouldn't this also change the meaning for anyone who creates a Request by hand, eg params = {'date_created': '1-1-14'}
req = Request('GET', 'http://jsonip.com', {'User-Agent': 'foobar'}, None, {}, params) where now suddenly params is JSON data? |
||
params=None, | ||
auth=None, | ||
cookies=None, | ||
|
@@ -230,6 +234,7 @@ def __init__(self, | |
self.headers = headers | ||
self.files = files | ||
self.data = data | ||
self.json = json | ||
self.params = params | ||
self.auth = auth | ||
self.cookies = cookies | ||
|
@@ -246,6 +251,7 @@ def prepare(self): | |
headers=self.headers, | ||
files=self.files, | ||
data=self.data, | ||
json=self.json, | ||
params=self.params, | ||
auth=self.auth, | ||
cookies=self.cookies, | ||
|
@@ -289,14 +295,15 @@ def __init__(self): | |
self.hooks = default_hooks() | ||
|
||
def prepare(self, method=None, url=None, headers=None, files=None, | ||
data=None, params=None, auth=None, cookies=None, hooks=None): | ||
data=None, params=None, auth=None, cookies=None, hooks=None, | ||
json=None): | ||
"""Prepares the entire request with the given parameters.""" | ||
|
||
self.prepare_method(method) | ||
self.prepare_url(url, params) | ||
self.prepare_headers(headers) | ||
self.prepare_cookies(cookies) | ||
self.prepare_body(data, files) | ||
self.prepare_body(data, files, json) | ||
self.prepare_auth(auth, url) | ||
# Note that prepare_auth must be last to enable authentication schemes | ||
# such as OAuth to work on a fully prepared request. | ||
|
@@ -397,7 +404,7 @@ def prepare_headers(self, headers): | |
else: | ||
self.headers = CaseInsensitiveDict() | ||
|
||
def prepare_body(self, data, files): | ||
def prepare_body(self, data, files, json=None): | ||
"""Prepares the given HTTP body data.""" | ||
|
||
# Check if file, fo, generator, iterator. | ||
|
@@ -408,6 +415,10 @@ def prepare_body(self, data, files): | |
content_type = None | ||
length = None | ||
|
||
if json is not None: | ||
content_type = 'application/json' | ||
body = json_dumps(json) | ||
|
||
is_stream = all([ | ||
hasattr(data, '__iter__'), | ||
not isinstance(data, (basestring, list, tuple, dict)) | ||
|
@@ -433,7 +444,7 @@ def prepare_body(self, data, files): | |
if files: | ||
(body, content_type) = self._encode_files(files, data) | ||
else: | ||
if data: | ||
if data and json is None: | ||
body = self._encode_params(data) | ||
if isinstance(data, basestring) or hasattr(data, 'read'): | ||
content_type = None | ||
|
@@ -443,7 +454,7 @@ def prepare_body(self, data, files): | |
self.prepare_content_length(body) | ||
|
||
# Add content-type if it wasn't explicitly provided. | ||
if (content_type) and (not 'content-type' in self.headers): | ||
if content_type and ('content-type' not in self.headers): | ||
self.headers['Content-Type'] = content_type | ||
|
||
self.body = body | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -365,6 +365,7 @@ def prepare_request(self, request): | |
url=request.url, | ||
files=request.files, | ||
data=request.data, | ||
json=request.json, | ||
headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict), | ||
params=merge_setting(request.params, self.params), | ||
auth=merge_setting(auth, self.auth), | ||
|
@@ -376,6 +377,7 @@ def prepare_request(self, request): | |
def request(self, method, url, | ||
params=None, | ||
data=None, | ||
json=None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here. |
||
headers=None, | ||
cookies=None, | ||
files=None, | ||
|
@@ -396,6 +398,8 @@ def request(self, method, url, | |
string for the :class:`Request`. | ||
:param data: (optional) Dictionary or bytes to send in the body of the | ||
:class:`Request`. | ||
:param json: (optional) json to send in the body of the | ||
:class:`Request`. | ||
:param headers: (optional) Dictionary of HTTP Headers to send with the | ||
:class:`Request`. | ||
:param cookies: (optional) Dict or CookieJar object to send with the | ||
|
@@ -426,6 +430,7 @@ def request(self, method, url, | |
headers = headers, | ||
files = files, | ||
data = data or {}, | ||
json = json, | ||
params = params or {}, | ||
auth = auth, | ||
cookies = cookies, | ||
|
@@ -479,15 +484,16 @@ def head(self, url, **kwargs): | |
kwargs.setdefault('allow_redirects', False) | ||
return self.request('HEAD', url, **kwargs) | ||
|
||
def post(self, url, data=None, **kwargs): | ||
def post(self, url, data=None, json=None, **kwargs): | ||
"""Sends a POST request. Returns :class:`Response` object. | ||
|
||
:param url: URL for the new :class:`Request` object. | ||
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`. | ||
:param json: (optional) json to send in the body of the :class:`Request`. | ||
:param \*\*kwargs: Optional arguments that ``request`` takes. | ||
""" | ||
|
||
return self.request('POST', url, data=data, **kwargs) | ||
return self.request('POST', url, data=data, json=json, **kwargs) | ||
|
||
def put(self, url, data=None, **kwargs): | ||
"""Sends a PUT request. Returns :class:`Response` object. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would get passed through with the kwargs, right, so I guess this is just so people know about the parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kevinburke yes