Created
January 9, 2014 19:25
-
-
Save suda/8340281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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