More liberal default JSON Accept header

Closes #470
This commit is contained in:
Jakub Roztocil 2016-07-02 14:18:36 +02:00
parent 41e822ca2f
commit 49a0fb6e0f
4 changed files with 20 additions and 17 deletions

View File

@ -15,6 +15,8 @@ This project adheres to `Semantic Versioning <http://semver.org/>`_.
* Added the ability to unset a request header with ``Header:``, and send an
empty value with ``Header;``.
* Added ``--default-scheme <URL_SCHEME>``.
* Changed the default JSON ``Accept`` header from ``application/json``
to ``application/json, */*``.
`0.9.4`_ (2016-07-01)

View File

@ -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

View File

@ -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

View File

@ -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']