1
0
mirror of https://github.com/httpie/cli.git synced 2025-08-10 22:42:05 +02:00

Fix --raw with --chunked (#1254)

* Fix --raw with --chunked

* Better naming / annotations

* More annotations
This commit is contained in:
Batuhan Taskaya
2021-12-29 12:41:44 +03:00
committed by GitHub
parent 0e10e23dca
commit 4c56d894ba
4 changed files with 133 additions and 75 deletions

View File

@@ -4,7 +4,7 @@ import json
import sys
from contextlib import contextmanager
from pathlib import Path
from typing import Callable, Iterable
from typing import Any, Dict, Callable, Iterable
from urllib.parse import urlparse, urlunparse
import requests
@@ -273,6 +273,24 @@ def make_send_kwargs_mergeable_from_env(args: argparse.Namespace) -> dict:
}
def json_dict_to_request_body(data: Dict[str, Any]) -> str:
# Propagate the top-level list if there is only one
# item in the object, with an en empty key.
if len(data) == 1:
[(key, value)] = data.items()
if key == '' and isinstance(value, list):
data = value
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 = ''
return data
def make_request_kwargs(
args: argparse.Namespace,
base_headers: HTTPHeadersDict = None,
@@ -287,19 +305,7 @@ def make_request_kwargs(
data = args.data
auto_json = data and not args.form
if (args.json or auto_json) and isinstance(data, dict):
# Propagate the top-level list if there is only one
# item in the object, with an en empty key.
if len(data) == 1:
[(key, value)] = data.items()
if key == '' and isinstance(value, list):
data = value
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 = ''
data = json_dict_to_request_body(data)
# Finalize headers.
headers = make_default_headers(args)
@@ -324,7 +330,7 @@ def make_request_kwargs(
'url': args.url,
'headers': headers,
'data': prepare_request_body(
body=data,
data,
body_read_callback=request_body_read_callback,
chunked=args.chunked,
offline=args.offline,