Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions requestbin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Request(object):
def __init__(self, input=None):
if input:
self.id = tinyid(6)
self.url = input.url
self.time = time.time()
self.remote_addr = input.headers.get('X-Forwarded-For', input.remote_addr)
self.method = input.method
Expand Down Expand Up @@ -105,6 +106,7 @@ def as_string(self, bytes):
def to_dict(self):
return dict(
id=self.id,
url=self.url,
time=self.time,
remote_addr=self.remote_addr,
method=self.method,
Expand All @@ -118,6 +120,20 @@ def to_dict(self):
content_type=self.content_type,
)

@property
def to_curl(self):
curl_command = f"curl -X {self.method} '{self.url}'"
curl_headers = "\\\n".join([
f" -H '{header}: {value}'"
for header, value in self.headers.items()
if header.lower() not in ['host', 'content-length']
])
if curl_headers:
curl_command += f"\\\n{curl_headers}"
if self.raw:
curl_command += f"\\\n -d '{self.raw}'"
return curl_command

@property
def created(self):
return datetime.datetime.fromtimestamp(self.time)
Expand Down
11 changes: 11 additions & 0 deletions requestbin/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,14 @@ input.xxlarge {
font-size: 42px;
height: auto;
}

.curl-preview {
display: none;
position: absolute;
right: 20px;
z-index: 100;
background: white;
border: 1px solid #ccc;
padding: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
11 changes: 11 additions & 0 deletions requestbin/static/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function toggleHovercard(id) {
event.preventDefault();
var hovercard = document.getElementById('hovercard-' + id);
hovercard.style.display = ( hovercard.style.display === 'none' || hovercard.style.display == '' ) ? 'block' : 'none';
}

function copyToClipboard(id) {
var hovercard = document.getElementById('hovercard-' + id);
var text = hovercard.querySelector('.code-preview').innerText;
navigator.clipboard.writeText(text);
}
15 changes: 14 additions & 1 deletion requestbin/templates/bin.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<span class="method">{{request.method}}</span>
<span class="absolute-path">{{request.path}}</span><span class="querystring">{{request.query_string|to_qs}}</span>
</div>
<div class="span6 content">
<div class="span5 content">
{% if request.content_type %}<i class="icon-code"></i>{% endif %} {{request.content_type}}<br>
<i class="icon-cloud-upload"></i> {{request.content_length|friendly_size}}
</div>
Expand All @@ -33,6 +33,19 @@
</span><br>
From {{request.remote_addr}}
</div>

<div class="span1" class="timestamp">
<a href="#detail-{{request.id}}" class="btn btn-mini btn-info" onclick="toggleHovercard('{{request.id}}')">
Copy <i class="icon-code"></i>
</a>
<div id="hovercard-{{request.id}}" class="hovercard curl-preview">
<pre class="code-preview">{{request.to_curl}}</pre>
<button class="btn btn-small" onclick="copyToClipboard('{{request.id}}')">
Copy to clipboard
</button>
</div>
</div>

</div>
</div>

Expand Down
1 change: 1 addition & 0 deletions requestbin/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<link type="text/css" href="/static/css/prettify.css" rel="stylesheet" />
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/prettify.js"></script>
<script type="text/javascript" src="/static/js/app.js"></script>
{% block head %}{% endblock %}
</head>
<body onload="prettyPrint()">
Expand Down
Loading