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_processor argument in curlify(), requests or httpx.

Parameters:

response (Response) – The result of performing a request.

Return type:

bytes

Returns:

response.content as is.

iripau.curlify.hide_content(response)[source]

Always return *** as bytes.

Hint

This function can be used as the value for the output_processor argument in curlify(), requests or httpx.

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_processor argument in curlify(), requests or httpx.

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 curl subprocess. The command and output can be echoed and/or sent to files as described in subprocess.run() and subprocess.Popen. At the end of the curl command, 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. If True, the argument --compressed will be added to the curl command.

  • verify (bool) – Whether or not the request disabled TLS certificate verification. If False, the argument --insecure will be added to the curl command.

  • pretty (bool) – Break the curl command into several lines.

  • output_processor (Callable[[Response], str | bytes]) – The return value of this function will be used as the stdout of the curl command. The default is raw_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 final curl command. If the header was not used in the request, ignore it.

  • headers_to_omit (Iterable[str]) – These headers won’t be in the final curl command. If the header was not used in the request, ignore it.

  • stdout_tees (Iterable[Union[IOBase, Callable[[], IOBase]]]) – The same as in subprocess.Popen.

  • stderr_tees (Iterable[Union[IOBase, Callable[[], IOBase]]]) – The same as in subprocess.Popen.

  • prompt_tees (Iterable[Union[IOBase, Callable[[], IOBase]]]) – The same as in subprocess.Popen.

  • add_global_stdout_tees (bool) – The same as in subprocess.Popen.

  • add_global_stderr_tees (bool) – The same as in subprocess.Popen.

  • add_global_prompt_tees (bool) – The same as in subprocess.Popen.

  • echo (bool) – The same as in subprocess.Popen.

Example

Perform a request and use the response to echo the simulated curl command 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

requests might fill in some headers with deduced or default values for every request.

I the example above, the headers User-Agent, Accept-Encoding, Accept and Connection were added by the original requests module.