Adds json parameter for POST requests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Carol Willing · committed by Carol Willing · parent d22b8d8 · Browse files · 4 files·+36 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+36 added−10 removed
- other:+36 added−10 removed
Provenance
- Commit
8f17741- Tree
1d16bc4- Author
Carol Willing <carolcode@willingconsulting.com>- Committer
Carol Willing <carolcode@willingconsulting.com>- Parent
d22b8d8
4 files changed+36 added−10 removed
modified:requests/api.py+4 added-2 removed
20 unmodified lines21 :param method: method for the new :class:`Request` object.22 :param url: URL for the new :class:`Request` object.23 :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.24 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.25+ :param json: (optional) json data to send in the body of the :class:`Request`.26 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.27 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.28 :param files: (optional) Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload.29 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.47 unmodified lines77 kwargs.setdefault('allow_redirects', False)78 return request('head', url, **kwargs)79 80 80-def post(url, data=None, **kwargs):81+def post(url, data=None, json=None, **kwargs):82 """Sends a POST request. Returns :class:`Response` object.83 84 :param url: URL for the new :class:`Request` object.85 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.86+ :param json: (optional) json data to send in the body of the :class:`Request`.87 :param \*\*kwargs: Optional arguments that ``request`` takes.88 """89 88- return request('post', url, data=data, **kwargs)90+ return request('post', url, data=data, json=json, **kwargs)91 92 93 def put(url, data=None, **kwargs):94 """Sends a PUT request. Returns :class:`Response` object.28 unmodified linesmodified:requests/models.py+16 added-6 removed
188 unmodified lines189 :param url: URL to send.190 :param headers: dictionary of headers to send.191 :param files: dictionary of {filename: fileobject} files to multipart upload.192 :param data: the body to attach the request. If a dictionary is provided, form-encoding will take place.193+ :param json: json for the body to attach the request.194 :param params: dictionary of URL parameters to append to the URL.195 :param auth: Auth handler or (user, pass) tuple.196 :param cookies: dictionary or CookieJar of cookies to attach to this request.197 :param hooks: dictionary of callback hooks, for internal usage.11 unmodified lines209 url=None,210 headers=None,211 files=None,212 data=None,213+ json=None,214 params=None,215 auth=None,216 cookies=None,217 hooks=None):218 219 # Default empty dicts for dict params.220 data = [] if data is None else data221+ json = [] if json is None else json222 files = [] if files is None else files223 headers = {} if headers is None else headers224 params = {} if params is None else params225 hooks = {} if hooks is None else hooks6 unmodified lines232 self.url = url233 self.headers = headers234 self.files = files235 self.data = data236+ self.json = json237 self.params = params238 self.auth = auth239 self.cookies = cookies240 8 unmodified lines249 url=self.url,250 headers=self.headers,251 files=self.files,252 data=self.data,253+ json=self.json,254 params=self.params,255 auth=self.auth,256 cookies=self.cookies,257 hooks=self.hooks,35 unmodified lines293 #: dictionary of callback hooks, for internal usage.294 self.hooks = default_hooks()295 296 def prepare(self, method=None, url=None, headers=None, files=None,292- data=None, params=None, auth=None, cookies=None, hooks=None):297+ data=None, json=None, params=None, auth=None, cookies=None, hooks=None):298 """Prepares the entire request with the given parameters."""299 300 self.prepare_method(method)301 self.prepare_url(url, params)99 unmodified lines401 self.headers = CaseInsensitiveDict((to_native_string(name), value) for name, value in headers.items())402 else:403 self.headers = CaseInsensitiveDict()404 400- def prepare_body(self, data, files):405+ def prepare_body(self, data, files, _json=None):406 """Prepares the given HTTP body data."""407 408 # Check if file, fo, generator, iterator.409 # If not, run through normal process.2 unmodified lines412 body = None413 content_type = None414 length = None415 416+ if _json is not None:417+ content_type = 'application/json'418+ data = json.dumps(_json)419+420 is_stream = all([421 hasattr(data, '__iter__'),422 not isinstance(data, (basestring, list, tuple, dict))423 ])19 unmodified lines443 (body, content_type) = self._encode_files(files, data)444 else:445 if data:446 body = self._encode_params(data)447+ if not _json:438- if isinstance(data, basestring) or hasattr(data, 'read'):439- content_type = None440- else:441- content_type = 'application/x-www-form-urlencoded'448+ if isinstance(data, basestring) or hasattr(data, 'read'):449+ content_type = None450+ else:451+ content_type = 'application/x-www-form-urlencoded'452 453 self.prepare_content_length(body)454 455 # Add content-type if it wasn't explicitly provided.371 unmodified linesmodified:requests/sessions.py+8 added-2 removed
363 unmodified lines364 method=request.method.upper(),365 url=request.url,366 files=request.files,367 data=request.data,368+ json=request.json,369 headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict),370 params=merge_setting(request.params, self.params),371 auth=merge_setting(auth, self.auth),372 cookies=merged_cookies,3 unmodified lines376 377 def request(self, method, url,378 params=None,379 data=None,380+ json=None,381 headers=None,382 cookies=None,383 files=None,384 auth=None,12 unmodified lines397 :param params: (optional) Dictionary or bytes to be sent in the query398 string for the :class:`Request`.399 :param data: (optional) Dictionary or bytes to send in the body of the400 :class:`Request`.401+ :param json: (optional) json to send in the body of the402+ :class:`Request`.403 :param headers: (optional) Dictionary of HTTP Headers to send with the404 :class:`Request`.405 :param cookies: (optional) Dict or CookieJar object to send with the406 :class:`Request`.22 unmodified lines429 url = url,430 headers = headers,431 files = files,432 data = data or {},433+ json = json or {},434 params = params or {},435 auth = auth,436 cookies = cookies,437 hooks = hooks,45 unmodified lines483 484 kwargs.setdefault('allow_redirects', False)485 return self.request('HEAD', url, **kwargs)486 482- def post(self, url, data=None, **kwargs):487+ def post(self, url, data=None, json=None, **kwargs):488 """Sends a POST request. Returns :class:`Response` object.489 490 :param url: URL for the new :class:`Request` object.491 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.492+ :param json: (optional) json to send in the body of the :class:`Request`.493 :param \*\*kwargs: Optional arguments that ``request`` takes.494 """495 490- return self.request('POST', url, data=data, **kwargs)496+ return self.request('POST', url, data=data, json=json, **kwargs)497 498 def put(self, url, data=None, **kwargs):499 """Sends a PUT request. Returns :class:`Response` object.500 162 unmodified linesmodified:test_requests.py+8 added
984 unmodified lines985 for item in r.history:986 assert item.history == total[0:i]987 i=i+1988 989+ def test_json_param_post_content_type_works(self):990+ r = requests.post(991+ httpbin('post'),992+ json={'life': 42}993+ )994+ assert r.status_code == 200995+ assert 'application/json' in r.headers['Content-Type']996 997+998 class TestContentEncodingDetection(unittest.TestCase):999 1000 def test_none(self):1001 encodings = requests.utils.get_encodings_from_content('')488 unmodified lines