From 45ce446017f355981de72dbcd10feb32e1aaf711 Mon Sep 17 00:00:00 2001 From: Jake Basile Date: Sat, 14 Apr 2012 14:13:53 -0500 Subject: [PATCH] -j/--json now adds "Accept": "application/json" to GET requests if no previous Accept header exists. --- httpie/__main__.py | 2 ++ tests/tests.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/httpie/__main__.py b/httpie/__main__.py index d7b12067..1759d1a0 100644 --- a/httpie/__main__.py +++ b/httpie/__main__.py @@ -124,6 +124,8 @@ def main(args=None, # JSON/Form content type. if args.json or (not args.form and data): + if args.method.lower() == 'get' and 'Accept' not in headers: + headers['Accept'] = 'application/json' if stdin_isatty: data = json.dumps(data) if not files and ('Content-Type' not in headers and (data or args.json)): diff --git a/tests/tests.py b/tests/tests.py index 0570d446..cf5a0959 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -94,6 +94,10 @@ class TestHTTPie(BaseTest): def test_json(self): response = http('POST', 'http://httpbin.org/post', 'foo=bar') self.assertIn('"foo": "bar"', response) + response2 = http('-j', 'GET', 'http://httpbin.org/headers') + self.assertIn('"Accept": "application/json"', response2) + response3 = http('-j', 'GET', 'http://httpbin.org/headers', 'Accept:application/xml') + self.assertIn('"Accept": "application/xml"', response3) def test_form(self): response = http('--form', 'POST', 'http://httpbin.org/post', 'foo=bar')