4646CONTENT_CHUNK_SIZE = 10 * 1024
4747ITER_CHUNK_SIZE = 512
4848
49+ json_dumps = json .dumps
50+
4951
5052class RequestEncodingMixin (object ):
5153 @property
@@ -189,7 +191,8 @@ class Request(RequestHooksMixin):
189191 :param url: URL to send.
190192 :param headers: dictionary of headers to send.
191193 :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).
193196 :param params: dictionary of URL parameters to append to the URL.
194197 :param auth: Auth handler or (user, pass) tuple.
195198 :param cookies: dictionary or CookieJar of cookies to attach to this request.
@@ -209,6 +212,7 @@ def __init__(self,
209212 headers = None ,
210213 files = None ,
211214 data = None ,
215+ json = None ,
212216 params = None ,
213217 auth = None ,
214218 cookies = None ,
@@ -230,6 +234,7 @@ def __init__(self,
230234 self .headers = headers
231235 self .files = files
232236 self .data = data
237+ self .json = json
233238 self .params = params
234239 self .auth = auth
235240 self .cookies = cookies
@@ -246,6 +251,7 @@ def prepare(self):
246251 headers = self .headers ,
247252 files = self .files ,
248253 data = self .data ,
254+ json = self .json ,
249255 params = self .params ,
250256 auth = self .auth ,
251257 cookies = self .cookies ,
@@ -289,14 +295,15 @@ def __init__(self):
289295 self .hooks = default_hooks ()
290296
291297 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 ):
293300 """Prepares the entire request with the given parameters."""
294301
295302 self .prepare_method (method )
296303 self .prepare_url (url , params )
297304 self .prepare_headers (headers )
298305 self .prepare_cookies (cookies )
299- self .prepare_body (data , files )
306+ self .prepare_body (data , files , json )
300307 self .prepare_auth (auth , url )
301308 # Note that prepare_auth must be last to enable authentication schemes
302309 # such as OAuth to work on a fully prepared request.
@@ -400,7 +407,7 @@ def prepare_headers(self, headers):
400407 else :
401408 self .headers = CaseInsensitiveDict ()
402409
403- def prepare_body (self , data , files ):
410+ def prepare_body (self , data , files , json = None ):
404411 """Prepares the given HTTP body data."""
405412
406413 # Check if file, fo, generator, iterator.
@@ -411,6 +418,10 @@ def prepare_body(self, data, files):
411418 content_type = None
412419 length = None
413420
421+ if json is not None :
422+ content_type = 'application/json'
423+ body = json_dumps (json )
424+
414425 is_stream = all ([
415426 hasattr (data , '__iter__' ),
416427 not isinstance (data , (basestring , list , tuple , dict ))
@@ -436,7 +447,7 @@ def prepare_body(self, data, files):
436447 if files :
437448 (body , content_type ) = self ._encode_files (files , data )
438449 else :
439- if data :
450+ if data and json is None :
440451 body = self ._encode_params (data )
441452 if isinstance (data , basestring ) or hasattr (data , 'read' ):
442453 content_type = None
@@ -446,7 +457,7 @@ def prepare_body(self, data, files):
446457 self .prepare_content_length (body )
447458
448459 # 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 ):
450461 self .headers ['Content-Type' ] = content_type
451462
452463 self .body = body
0 commit comments