From 49a0fb6e0fb4fa130db9c5a2c4d3b27a6579cc75 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sat, 2 Jul 2016 14:18:36 +0200 Subject: [PATCH] More liberal default JSON Accept header Closes #470 --- CHANGELOG.rst | 2 ++ README.rst | 12 ++++++------ httpie/client.py | 12 ++++++------ tests/test_defaults.py | 11 ++++++----- 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 9cb4120f..ae022955 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,8 @@ This project adheres to `Semantic Versioning `_. * Added the ability to unset a request header with ``Header:``, and send an empty value with ``Header;``. * Added ``--default-scheme ``. +* Changed the default JSON ``Accept`` header from ``application/json`` + to ``application/json, */*``. `0.9.4`_ (2016-07-01) diff --git a/README.rst b/README.rst index 6776af0c..6e8cb6d1 100644 --- a/README.rst +++ b/README.rst @@ -183,7 +183,7 @@ with `authentication`_: .. code-block:: bash - $ http -a USERNAME POST https://api.github.com/repos/jkbrzt/httpie/issues/83/comments body='HTTPie is awesome!' + $ http -a USERNAME POST https://api.github.com/repos/jkbrzt/httpie/issues/83/comments body='HTTPie is awesome! :heart:' Upload a file using `redirected input`_: @@ -403,13 +403,13 @@ both of which can be overwritten: ================ ======================================= ``Content-Type`` ``application/json`` -``Accept`` ``application/json`` +``Accept`` ``application/json, */*`` ================ ======================================= You can use ``--json, -j`` to explicitly set ``Accept`` to ``application/json`` regardless of whether you are sending data (it's a shortcut for setting the header via the usual header notation – -``http url Accept:application/json``). Additionally, +``http url Accept:application/json, */*``). Additionally, HTTPie will try to detect JSON responses even when the ``Content-Type`` is incorrectly ``text/plain`` or unknown. @@ -422,7 +422,7 @@ Simple example: .. code-block:: http PUT / HTTP/1.1 - Accept: application/json + Accept: application/json, */* Accept-Encoding: gzip, deflate Content-Type: application/json Host: example.org @@ -449,7 +449,7 @@ fields using ``=@`` and ``:=@``: .. code-block:: http PUT /person/1 HTTP/1.1 - Accept: application/json + Accept: application/json, */* Content-Type: application/json Host: api.example.com @@ -825,7 +825,7 @@ documentation examples: $ http --verbose PUT httpbin.org/put hello=world PUT /put HTTP/1.1 - Accept: application/json + Accept: application/json, */* Accept-Encoding: gzip, deflate Content-Type: application/json Host: httpbin.org diff --git a/httpie/client.py b/httpie/client.py index e8a9ef30..59bd9bee 100644 --- a/httpie/client.py +++ b/httpie/client.py @@ -24,8 +24,9 @@ except AttributeError: pass -FORM = 'application/x-www-form-urlencoded; charset=utf-8' -JSON = 'application/json' +FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8' +JSON_CONTENT_TYPE = 'application/json' +JSON_ACCEPT = '{0}, */*'.format(JSON_CONTENT_TYPE) DEFAULT_UA = 'HTTPie/%s' % __version__ @@ -100,16 +101,15 @@ def get_default_headers(args): } auto_json = args.data and not args.form - # FIXME: Accept is set to JSON with `http url @./file.txt`. if args.json or auto_json: - default_headers['Accept'] = 'application/json' + default_headers['Accept'] = JSON_ACCEPT if args.json or (auto_json and args.data): - default_headers['Content-Type'] = JSON + default_headers['Content-Type'] = JSON_CONTENT_TYPE elif args.form and not args.files: # If sending files, `requests` will set # the `Content-Type` for us. - default_headers['Content-Type'] = FORM + default_headers['Content-Type'] = FORM_CONTENT_TYPE return default_headers diff --git a/tests/test_defaults.py b/tests/test_defaults.py index a82a1151..49d5749e 100644 --- a/tests/test_defaults.py +++ b/tests/test_defaults.py @@ -2,6 +2,7 @@ Tests for the provided defaults regarding HTTP method, and --json vs. --form. """ +from httpie.client import JSON_ACCEPT from utils import TestEnvironment, http, HTTP_OK from fixtures import FILE_PATH @@ -58,20 +59,20 @@ class TestAutoContentTypeAndAcceptHeaders: def test_POST_with_data_auto_JSON_headers(self, httpbin): r = http('POST', httpbin.url + '/post', 'a=b') assert HTTP_OK in r - assert '"Accept": "application/json"' in r - assert '"Content-Type": "application/json' in r + assert r.json['headers']['Accept'] == JSON_ACCEPT + assert r.json['headers']['Content-Type'] == 'application/json' def test_GET_with_data_auto_JSON_headers(self, httpbin): # JSON headers should automatically be set also for GET with data. r = http('POST', httpbin.url + '/post', 'a=b') assert HTTP_OK in r - assert '"Accept": "application/json"' in r, r - assert '"Content-Type": "application/json' in r + assert r.json['headers']['Accept'] == JSON_ACCEPT + assert r.json['headers']['Content-Type'] == 'application/json' def test_POST_explicit_JSON_auto_JSON_accept(self, httpbin): r = http('--json', 'POST', httpbin.url + '/post') assert HTTP_OK in r - assert r.json['headers']['Accept'] == 'application/json' + assert r.json['headers']['Accept'] == JSON_ACCEPT # Make sure Content-Type gets set even with no data. # https://github.com/jkbrzt/httpie/issues/137 assert 'application/json' in r.json['headers']['Content-Type']