1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00

Set JSON Content-Type only with data even with -j.

This commit is contained in:
Jakub Roztocil 2012-08-06 22:14:52 +02:00
parent 4e58a3849a
commit 44e409693b
4 changed files with 11 additions and 8 deletions

View File

@ -33,7 +33,7 @@ def get_response(args):
auto_json = args.data and not args.form
if args.json or auto_json:
if 'Content-Type' not in args.headers:
if 'Content-Type' not in args.headers and args.data:
args.headers['Content-Type'] = JSON
if 'Accept' not in args.headers:

View File

@ -32,6 +32,8 @@ class Environment(object):
colors = 256 if '256color' in os.environ.get('TERM', '') else 88
def __init__(self, **kwargs):
assert all(hasattr(type(self), attr)
for attr in kwargs.keys())
self.__dict__.update(**kwargs)
@ -74,7 +76,7 @@ class HTTPMessage(object):
class HTTPResponse(HTTPMessage):
"""A `requests.models.Response` wrapper."""
"""A :class:`requests.models.Response` wrapper."""
def iter_body(self, chunk_size=1):
return self._orig.iter_content(chunk_size=chunk_size)
@ -106,7 +108,7 @@ class HTTPResponse(HTTPMessage):
class HTTPRequest(HTTPMessage):
"""A `requests.models.Request` wrapper."""
"""A :class:`requests.models.Request` wrapper."""
def iter_body(self, chunk_size):
yield self.body

View File

@ -102,8 +102,9 @@ def output_stream(args, env, request, response):
def make_stream(env, args):
"""Pick the right stream type for based on `env` and `args`.
and wrap it with a partial with the type-specific args.
"""Pick the right stream type based on `env` and `args`.
Wrap it in a partial with the type-specific args so that
we don't need to think what stream we are dealing with.
"""
if not env.stdout_isatty and not args.prettify:

View File

@ -386,15 +386,15 @@ class AutoContentTypeAndAcceptHeadersTest(BaseTestCase):
self.assertIn('"Accept": "application/json"', r)
self.assertIn('"Content-Type": "application/json; charset=utf-8', r)
def test_POST_explicit_JSON_auto_JSON_headers(self):
def test_POST_explicit_JSON_auto_JSON_accept(self):
r = http(
'--json',
'POST',
httpbin('/post')
)
self.assertIn(OK, r)
self.assertIn('"Accept": "application/json"', r)
self.assertIn('"Content-Type": "application/json; charset=utf-8', r)
self.assertEqual(r.json['headers']['Accept'], 'application/json')
self.assertNotIn('Content-Type', r.json['headers'])
def test_GET_explicit_JSON_explicit_headers(self):
r = http(