Skip to content

Instantly share code, notes, and snippets.

@suda
Created January 9, 2014 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suda/8340281 to your computer and use it in GitHub Desktop.
Save suda/8340281 to your computer and use it in GitHub Desktop.
"""
Simple Gist for debugging Django requests using RequestBin (http://requestb.in/)
Usage:
from request_bin import request_bin
def index_view(request):
request_bin('http://requestb.in/1fpixiy1', request)
Author:
Wojtek @suda Siudzinski <admin@suda.pl>
"""
import requests
import re
def request_bin(bin, request):
regex_http_ = re.compile(r'^HTTP_.+$')
regex_content_type = re.compile(r'^CONTENT_TYPE$')
regex_content_length = re.compile(r'^CONTENT_LENGTH$')
headers = {}
for header in request.META:
if regex_http_.match(header) or regex_content_type.match(header) or regex_content_length.match(header):
headers[header] = request.META[header]
kwargs = {
'params': request.GET,
'data': request.POST,
'headers': headers,
'files': request.FILES
}
if request.method == 'GET':
requests.get(bin, **kwargs)
elif request.method == 'POST':
requests.post(bin, **kwargs)
elif request.method == 'PUT':
requests.put(bin, **kwargs)
elif request.method == 'DELETE':
requests.delete(bin, **kwargs)
elif request.method == 'HEAD':
requests.head(bin, **kwargs)
elif request.method == 'OPTIONS':
requests.options(bin, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment