You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2025-08-10 22:42:05 +02:00
Make sure session and default headers play nice
Before: headers = default + args + session Now: headers = default + session + args Fixes #180
This commit is contained in:
@@ -18,26 +18,27 @@ DEFAULT_UA = 'HTTPie/%s' % __version__
|
||||
def get_response(args, config_dir):
|
||||
"""Send the request and return a `request.Response`."""
|
||||
|
||||
requests_kwargs = get_requests_kwargs(args)
|
||||
|
||||
if args.debug:
|
||||
sys.stderr.write('\n>>> requests.request(%s)\n\n'
|
||||
% pformat(requests_kwargs))
|
||||
|
||||
if not args.session and not args.session_read_only:
|
||||
requests_kwargs = get_requests_kwargs(args)
|
||||
if args.debug:
|
||||
dump_request(requests_kwargs)
|
||||
response = requests.request(**requests_kwargs)
|
||||
else:
|
||||
response = sessions.get_response(
|
||||
args=args,
|
||||
config_dir=config_dir,
|
||||
session_name=args.session or args.session_read_only,
|
||||
requests_kwargs=requests_kwargs,
|
||||
read_only=bool(args.session_read_only),
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def dump_request(kwargs):
|
||||
sys.stderr.write('\n>>> requests.request(%s)\n\n'
|
||||
% pformat(kwargs))
|
||||
|
||||
|
||||
def encode_headers(headers):
|
||||
# This allows for unicode headers which is non-standard but practical.
|
||||
# See: https://github.com/jakubroztocil/httpie/issues/212
|
||||
@@ -47,36 +48,47 @@ def encode_headers(headers):
|
||||
)
|
||||
|
||||
|
||||
def get_requests_kwargs(args):
|
||||
"""Translate our `args` into `requests.request` keyword arguments."""
|
||||
|
||||
implicit_headers = {
|
||||
def get_default_headers(args):
|
||||
default_headers = {
|
||||
'User-Agent': DEFAULT_UA
|
||||
}
|
||||
|
||||
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:
|
||||
implicit_headers['Accept'] = 'application/json'
|
||||
default_headers['Accept'] = 'application/json'
|
||||
if args.json or (auto_json and args.data):
|
||||
implicit_headers['Content-Type'] = JSON
|
||||
|
||||
if isinstance(args.data, dict):
|
||||
if args.data:
|
||||
args.data = json.dumps(args.data)
|
||||
else:
|
||||
# We need to set data to an empty string to prevent requests
|
||||
# from assigning an empty list to `response.request.data`.
|
||||
args.data = ''
|
||||
default_headers['Content-Type'] = JSON
|
||||
|
||||
elif args.form and not args.files:
|
||||
# If sending files, `requests` will set
|
||||
# the `Content-Type` for us.
|
||||
implicit_headers['Content-Type'] = FORM
|
||||
default_headers['Content-Type'] = FORM
|
||||
return default_headers
|
||||
|
||||
for name, value in implicit_headers.items():
|
||||
if name not in args.headers:
|
||||
args.headers[name] = value
|
||||
|
||||
def get_requests_kwargs(args, base_headers=None):
|
||||
"""
|
||||
Translate our `args` into `requests.request` keyword arguments.
|
||||
|
||||
"""
|
||||
# Serialize JSON data, if needed.
|
||||
data = args.data
|
||||
auto_json = data and not args.form
|
||||
if args.json or auto_json and isinstance(data, dict):
|
||||
if data:
|
||||
data = json.dumps(data)
|
||||
else:
|
||||
# We need to set data to an empty string to prevent requests
|
||||
# from assigning an empty list to `response.request.data`.
|
||||
data = ''
|
||||
|
||||
# Finalize headers.
|
||||
headers = get_default_headers(args)
|
||||
if base_headers:
|
||||
headers.update(base_headers)
|
||||
headers.update(args.headers)
|
||||
headers = encode_headers(headers)
|
||||
|
||||
credentials = None
|
||||
if args.auth:
|
||||
@@ -87,14 +99,14 @@ def get_requests_kwargs(args):
|
||||
if args.cert:
|
||||
cert = args.cert
|
||||
if args.certkey:
|
||||
cert = (cert, args.certkey)
|
||||
cert = cert, args.certkey
|
||||
|
||||
kwargs = {
|
||||
'stream': True,
|
||||
'method': args.method.lower(),
|
||||
'url': args.url,
|
||||
'headers': encode_headers(args.headers),
|
||||
'data': args.data,
|
||||
'headers': headers,
|
||||
'data': data,
|
||||
'verify': {
|
||||
'yes': True,
|
||||
'no': False
|
||||
|
Reference in New Issue
Block a user