Skip to content

Instantly share code, notes, and snippets.

@kevinburke
Last active April 28, 2022 23:38
Show Gist options
  • Save kevinburke/7990326 to your computer and use it in GitHub Desktop.
Save kevinburke/7990326 to your computer and use it in GitHub Desktop.
Turn a requests request back into (roughly) the HTTP request that went over the wire. Also translates the request into a curl command
curl -X{{ method }} {{ url }}
{%- if query_string|length %}?{% endif %}
{{- query_string }} \
{%- for header, value in headers.iteritems() %}
-H '{{ header|urlencode }}: {{ value|urlencode }}'{% if not loop.last or params|length or auth %} \{% endif %}
{%- endfor -%}
{% if params|length %}{% for param, value in params.iteritems() %}
-d '{{ param.decode('utf-8')|urlencode }}={{ value.decode('utf-8')|urlencode }}'{% if not loop.last or auth %} \{% endif %}
{%- endfor %}{%- endif %}
{%- if auth %}
-u {{ auth[0] }}:{{ auth[1] }}
{% endif %}
import urllib2
import json
from werkzeug.http import HTTP_STATUS_CODES
def render_request(request, message):
base_url = request.base_uri + request.path if not hasattr(request, 'base_url') else request.base_url
query_string = request.query_string
if isinstance(query_string, dict):
query_string = urllib.urlencode(query_string)
encoded_params = _get_params(request.form or {})
return render('request.jinja', message=message, method=request.method,
base_url=base_url, query_string=query_string,
params=encoded_params, headers=headers)
def render_response(response, message, application=None):
""" Render an internal response
XXX might want to clean responses from internal services
"""
if application is None:
application = current_app
if response.status_code in HTTP_STATUS_CODES:
code_text = HTTP_STATUS_CODES[response.status_code]
else:
code_text = 'Unknown'
if ('content-type' in response.headers and
('mp3' in response.headers['content-type'] or
'wav' in response.headers['content-type'] or
'mpeg' in response.headers['content-type'])):
content = '[Audio content]'
else:
# XXX, try/catch here
content = response.content.encode('utf-8')
if current_app and current_app.debug:
try:
# pretty print
content = json.dumps(json.loads(content), indent=4)
except Exception:
pass
return render('response.jinja', message=message, code_text=code_text,
status_code=response.status_code, headers=response.headers,
content=content)
def render_curl(request, auth):
""" Turn a HTTP request back into curl
This should be used mainly by the test client, not by production code
"""
if request.form is not None:
params = utf8_encode_dict(request.form)
params = {str(key): str(value) for key, value in params.iteritems()}
else:
params = {}
# ServiceRequest doesn't have this attribute
base_url = request.base_uri + request.path if not hasattr(request, 'base_url') else request.base_url
return render('curl.jinja', method=request.method, url=base_url,
query_string=request.query_string, params=params,
headers=request.headers, auth=auth)
>>> {{ message }}:
{{ method }} {{ base_url }}{% if query_string|length %}?{% endif %}{{ query_string }}
{% for header, value in headers.iteritems() %}{{ header }}: {{ value }}
{% endfor %}{% if params|length %}
{{ params }}
{% endif %}
>>> {{ message }}:
HTTP/1.1 {{ status_code }} {{ code_text }}
{% for header, value in headers.iteritems() %}{{ header }}: {{ value }}
{% endfor %}{% if content|length %}
{{ content }}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment