Change default JSON Accept to application/json, */*;q=0.5

Close #488
This commit is contained in:
Jakub Roztocil 2020-04-13 22:12:06 +02:00 committed by GitHub
parent 684a4708d7
commit cdf691c212
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 12 deletions

View File

@ -8,8 +8,13 @@ This project adheres to `Semantic Versioning <https://semver.org/>`_.
`2.1.0-dev`_ (unreleased) `2.1.0-dev`_ (unreleased)
------------------------- -------------------------
* Add ``--path-as-is`` to bypass dot segment (``/../`` or ``/./``) URL squashing. * Added ``--path-as-is`` to bypass dot segment (``/../`` or ``/./``)
* Fixed ``--form`` file upload mixed with redirected ``stdin`` error handling. URL squashing (#895).
* Changed the default value ``Accept`` header value for JSON requests from
``application/json, */*`` to ``application/json, */*;q=0.5``
to clearly indicate preference (#488).
* Fixed ``--form`` file upload mixed with redirected ``stdin`` error handling
(#840).
`2.0.0`_ (2020-01-12) `2.0.0`_ (2020-01-12)

View File

@ -469,7 +469,7 @@ Simple example:
.. code-block:: http .. code-block:: http
PUT / HTTP/1.1 PUT / HTTP/1.1
Accept: application/json, */* Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate Accept-Encoding: gzip, deflate
Content-Type: application/json Content-Type: application/json
Host: httpbin.org Host: httpbin.org
@ -490,7 +490,7 @@ both of which can be overwritten:
================ ======================================= ================ =======================================
``Content-Type`` ``application/json`` ``Content-Type`` ``application/json``
``Accept`` ``application/json, */*`` ``Accept`` ``application/json, */*;q=0.5``
================ ======================================= ================ =======================================
@ -500,7 +500,7 @@ Explicit JSON
You can use ``--json, -j`` to explicitly set ``Accept`` You can use ``--json, -j`` to explicitly set ``Accept``
to ``application/json`` regardless of whether you are sending data to ``application/json`` regardless of whether you are sending data
(it's a shortcut for setting the header via the usual header notation: (it's a shortcut for setting the header via the usual header notation:
``http url Accept:'application/json, */*'``). Additionally, ``http url Accept:'application/json, */*;q=0.5'``). Additionally,
HTTPie will try to detect JSON responses even when the HTTPie will try to detect JSON responses even when the
``Content-Type`` is incorrectly ``text/plain`` or unknown. ``Content-Type`` is incorrectly ``text/plain`` or unknown.
@ -525,7 +525,7 @@ fields using ``=@`` and ``:=@``:
.. code-block:: http .. code-block:: http
PUT /person/1 HTTP/1.1 PUT /person/1 HTTP/1.1
Accept: application/json, */* Accept: application/json, */*;q=0.5
Content-Type: application/json Content-Type: application/json
Host: httpbin.org Host: httpbin.org
@ -1009,7 +1009,7 @@ documentation examples:
$ http --verbose PUT httpbin.org/put hello=world $ http --verbose PUT httpbin.org/put hello=world
PUT /put HTTP/1.1 PUT /put HTTP/1.1
Accept: application/json, */* Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate Accept-Encoding: gzip, deflate
Content-Type: application/json Content-Type: application/json
Host: httpbin.org Host: httpbin.org

View File

@ -30,7 +30,7 @@ except (ImportError, AttributeError):
FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8' FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8'
JSON_CONTENT_TYPE = 'application/json' JSON_CONTENT_TYPE = 'application/json'
JSON_ACCEPT = f'{JSON_CONTENT_TYPE}, */*' JSON_ACCEPT = f'{JSON_CONTENT_TYPE}, */*;q=0.5'
DEFAULT_UA = f'HTTPie/{__version__}' DEFAULT_UA = f'HTTPie/{__version__}'

View File

@ -22,6 +22,7 @@ def test_default_headers_case_insensitive(httpbin):
assert 'Content-Type' not in r assert 'Content-Type' not in r
# noinspection PyPep8Naming
class TestImplicitHTTPMethod: class TestImplicitHTTPMethod:
def test_implicit_GET(self, httpbin): def test_implicit_GET(self, httpbin):
r = http(httpbin.url + '/get') r = http(httpbin.url + '/get')
@ -51,9 +52,9 @@ class TestImplicitHTTPMethod:
class TestAutoContentTypeAndAcceptHeaders: class TestAutoContentTypeAndAcceptHeaders:
""" """
Test that Accept and Content-Type correctly defaults to JSON, Test that `Accept` and `Content-Type` correctly default to JSON,
but can still be overridden. The same with Content-Type when --form but can still be overridden. The same with Content-Type when `--form`
-f is used. `-f` is used.
""" """
@ -84,7 +85,7 @@ class TestAutoContentTypeAndAcceptHeaders:
assert r.json['headers']['Accept'] == JSON_ACCEPT assert r.json['headers']['Accept'] == JSON_ACCEPT
assert r.json['headers']['Content-Type'] == 'application/json' assert r.json['headers']['Content-Type'] == 'application/json'
def test_POST_explicit_JSON_auto_JSON_accept(self, httpbin): def test_POST_explicit_JSON_JSON_ACCEPT(self, httpbin):
r = http('--json', 'POST', httpbin.url + '/post') r = http('--json', 'POST', httpbin.url + '/post')
assert HTTP_OK in r assert HTTP_OK in r
assert r.json['headers']['Accept'] == JSON_ACCEPT assert r.json['headers']['Accept'] == JSON_ACCEPT