iripau.curlify module¶
Simulate an HTTP request represented by a
requests.Response,
httpx.Response
or equivalent object was executed in a curl subprocess.
- iripau.curlify.raw_content(response)[source]¶
Just return the content of a response, either a string or bytes, without any post-processing.
Hint
This function can be used as the value for the
output_processorargument incurlify(),requestsorhttpx.- Parameters:
response (
Response) – The result of performing a request.- Return type:
bytes- Returns:
response.contentas is.
- iripau.curlify.hide_content(response)[source]¶
Always return
***as bytes.Hint
This function can be used as the value for the
output_processorargument incurlify(),requestsorhttpx.- Parameters:
response (
Response) – The result of performing a request.- Return type:
bytes- Returns:
b"***"
- iripau.curlify.try_json_content(response)[source]¶
Try to pretty-format the content of a response as JSON. If the content is not a valid JSON, return the raw content.
Hint
This function can be used as the value for the
output_processorargument incurlify(),requestsorhttpx.- Parameters:
response (
Response) – The result of performing a request.- Return type:
bytes- Returns:
The pretty JSON or the raw content.
- iripau.curlify.curlify(response, compressed=False, verify=True, pretty=False, output_processor=None, headers_to_hide=[], headers_to_omit=[], stdout_tees=[], add_global_stdout_tees=True, stderr_tees=[], add_global_stderr_tees=True, prompt_tees=[], add_global_prompt_tees=True, echo=None)[source]¶
Simulate the request was executed by a
curlsubprocess. The command and output can be echoed and/or sent to files as described insubprocess.run()andsubprocess.Popen. At the end of thecurlcommand, a comment with the HTTP status code and reason will be added.- Parameters:
response (
Response) – The result of performing a request.compressed (
bool) – Whether or not the request used compressed data. IfTrue, the argument--compressedwill be added to thecurlcommand.verify (
bool) – Whether or not the request disabled TLS certificate verification. IfFalse, the argument--insecurewill be added to thecurlcommand.pretty (
bool) – Break thecurlcommand into several lines.output_processor (
Callable[[Response],str|bytes]) – The return value of this function will be used as thestdoutof thecurlcommand. The default israw_content(), which returns whatever the content of the response is.headers_to_hide (
Iterable[str]) – The value of these headers will be replaced with***in the finalcurlcommand. If the header was not used in the request, ignore it.headers_to_omit (
Iterable[str]) – These headers won’t be in the finalcurlcommand. If the header was not used in the request, ignore it.stdout_tees (
Iterable[Union[IOBase,Callable[[],IOBase]]]) – The same as insubprocess.Popen.stderr_tees (
Iterable[Union[IOBase,Callable[[],IOBase]]]) – The same as insubprocess.Popen.prompt_tees (
Iterable[Union[IOBase,Callable[[],IOBase]]]) – The same as insubprocess.Popen.add_global_stdout_tees (
bool) – The same as insubprocess.Popen.add_global_stderr_tees (
bool) – The same as insubprocess.Popen.add_global_prompt_tees (
bool) – The same as insubprocess.Popen.echo (
bool) – The same as insubprocess.Popen.
Example
Perform a request and use the response to echo the simulated
curlcommand and its output into the terminal:import requests response = requests.get("https://dummyjson.com/test", verify=False) curlify(response, echo=True) # $ curl -H 'User-Agent: python-requests/2.32.3' -H 'Accept-Encoding: gzip, deflate' -H 'Accept: */*' -H 'Connection: keep-alive' https://dummyjson.com/test # 200 - OK # {"status":"ok","method":"GET"}
Attention
requestsmight fill in some headers with deduced or default values for every request.I the example above, the headers
User-Agent,Accept-Encoding,AcceptandConnectionwere added by the originalrequestsmodule.