Skip to content
Myr

Merge pull request #2258 from willingc/add-json

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add json parameter

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 20ca9fe, 8ed941f · Browse files · 4 files+38 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+38 added−10 removed

  • other:+38 added−10 removed

4 files+38 added−10 removed

modified:requests/api.py+4 added-2 removed
20 unmodified lines
21 :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.
51 unmodified lines
81 kwargs.setdefault('allow_redirects', False)82 return request('head', url, **kwargs)838484def post(url, data=None, **kwargs):85def post(url, data=None, json=None, **kwargs):86 """Sends a POST request. Returns :class:`Response` object.8788 :param url: URL for the new :class:`Request` object.89 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.90 :param json: (optional) json data to send in the body of the :class:`Request`.91 :param \*\*kwargs: Optional arguments that ``request`` takes.92 """9392 return request('post', url, data=data, **kwargs)94 return request('post', url, data=data, json=json, **kwargs)959697def put(url, data=None, **kwargs):98 """Sends a PUT request. Returns :class:`Response` object.
28 unmodified lines
modified:requests/models.py+17 added-6 removed
44 unmodified lines
45DEFAULT_REDIRECT_LIMIT = 3046CONTENT_CHUNK_SIZE = 10 * 102447ITER_CHUNK_SIZE = 5124849json_dumps = json.dumps505152class RequestEncodingMixin(object):53 @property54 def path_url(self):55 """Build the path URL to use."""
134 unmodified lines
190 :param method: HTTP method to use.191 :param url: URL to send.192 :param headers: dictionary of headers to send.193 :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.194 :param data: the body to attach to the request. If a dictionary is provided, form-encoding will take place.195 :param json: json for the body to attach to the request (if data is not specified).196 :param params: dictionary of URL parameters to append to the URL.197 :param auth: Auth handler or (user, pass) tuple.198 :param cookies: dictionary or CookieJar of cookies to attach to this request.199 :param hooks: dictionary of callback hooks, for internal usage.
11 unmodified lines
211 url=None,212 headers=None,213 files=None,214 data=None,215 json=None,216 params=None,217 auth=None,218 cookies=None,219 hooks=None):
13 unmodified lines
233 self.url = url234 self.headers = headers235 self.files = files236 self.data = data237 self.json = json238 self.params = params239 self.auth = auth240 self.cookies = cookies241
8 unmodified lines
250 url=self.url,251 headers=self.headers,252 files=self.files,253 data=self.data,254 json=self.json,255 params=self.params,256 auth=self.auth,257 cookies=self.cookies,258 hooks=self.hooks,
35 unmodified lines
294 #: dictionary of callback hooks, for internal usage.295 self.hooks = default_hooks()296297 def prepare(self, method=None, url=None, headers=None, files=None,292 data=None, params=None, auth=None, cookies=None, hooks=None):298 data=None, params=None, auth=None, cookies=None, hooks=None,299 json=None):300 """Prepares the entire request with the given parameters."""301302 self.prepare_method(method)303 self.prepare_url(url, params)304 self.prepare_headers(headers)305 self.prepare_cookies(cookies)299 self.prepare_body(data, files)306 self.prepare_body(data, files, json)307 self.prepare_auth(auth, url)308 # Note that prepare_auth must be last to enable authentication schemes309 # such as OAuth to work on a fully prepared request.310
95 unmodified lines
406 self.headers = CaseInsensitiveDict((to_native_string(name), value) for name, value in headers.items())407 else:408 self.headers = CaseInsensitiveDict()409403 def prepare_body(self, data, files):410 def prepare_body(self, data, files, json=None):411 """Prepares the given HTTP body data."""412413 # Check if file, fo, generator, iterator.414 # If not, run through normal process.
2 unmodified lines
417 body = None418 content_type = None419 length = None420421 if json is not None:422 content_type = 'application/json'423 body = json_dumps(json)424425 is_stream = all([426 hasattr(data, '__iter__'),427 not isinstance(data, (basestring, list, tuple, dict))428 ])
17 unmodified lines
446 # Multi-part file uploads.447 if files:448 (body, content_type) = self._encode_files(files, data)449 else:439 if data:450 if data and json is None:451 body = self._encode_params(data)452 if isinstance(data, basestring) or hasattr(data, 'read'):453 content_type = None454 else:
1 unmodified line
456457 self.prepare_content_length(body)458459 # Add content-type if it wasn't explicitly provided.449 if (content_type) and (not 'content-type' in self.headers):460 if content_type and ('content-type' not in self.headers):461 self.headers['Content-Type'] = content_type462463 self.body = body464
366 unmodified lines
modified:requests/sessions.py+8 added-2 removed
364 unmodified lines
365 method=request.method.upper(),366 url=request.url,367 files=request.files,368 data=request.data,369 json=request.json,370 headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict),371 params=merge_setting(request.params, self.params),372 auth=merge_setting(auth, self.auth),373 cookies=merged_cookies,
3 unmodified lines
377378 def request(self, method, url,379 params=None,380 data=None,381 json=None,382 headers=None,383 cookies=None,384 files=None,385 auth=None,
12 unmodified lines
398 :param params: (optional) Dictionary or bytes to be sent in the query399 string for the :class:`Request`.400 :param data: (optional) Dictionary or bytes to send in the body of the401 :class:`Request`.402 :param json: (optional) json to send in the body of the403 :class:`Request`.404 :param headers: (optional) Dictionary of HTTP Headers to send with the405 :class:`Request`.406 :param cookies: (optional) Dict or CookieJar object to send with the407 :class:`Request`.
25 unmodified lines
433 url = url,434 headers = headers,435 files = files,436 data = data or {},437 json = json,438 params = params or {},439 auth = auth,440 cookies = cookies,441 hooks = hooks,
45 unmodified lines
487488 kwargs.setdefault('allow_redirects', False)489 return self.request('HEAD', url, **kwargs)490486 def post(self, url, data=None, **kwargs):491 def post(self, url, data=None, json=None, **kwargs):492 """Sends a POST request. Returns :class:`Response` object.493494 :param url: URL for the new :class:`Request` object.495 :param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.496 :param json: (optional) json to send in the body of the :class:`Request`.497 :param \*\*kwargs: Optional arguments that ``request`` takes.498 """499494 return self.request('POST', url, data=data, **kwargs)500 return self.request('POST', url, data=data, json=json, **kwargs)501502 def put(self, url, data=None, **kwargs):503 """Sends a PUT request. Returns :class:`Response` object.504
167 unmodified lines
modified:test_requests.py+9 added
1005 unmodified lines
1006 for item in r.history:1007 assert item.history == total[0:i]1008 i=i+110091010 def test_json_param_post_content_type_works(self):1011 r = requests.post(1012 httpbin('post'),1013 json={'life': 42}1014 )1015 assert r.status_code == 2001016 assert 'application/json' in r.request.headers['Content-Type']1017 assert {'life': 42} == r.json()['json']101810191020class TestContentEncodingDetection(unittest.TestCase):10211022 def test_none(self):1023 encodings = requests.utils.get_encodings_from_content('')
488 unmodified lines