Skip to content

Commit

Permalink
Adding request body and curl command to sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
tcz committed Mar 17, 2016
1 parent 2a0f95e commit aef6a89
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
66 changes: 58 additions & 8 deletions Resources/views/layout.html.twig
Expand Up @@ -337,12 +337,22 @@
};
var displayFinalUrl = function(xhr, method, url, data, container) {
if ('GET' == method && !jQuery.isEmptyObject(data)) {
var separator = url.indexOf('?') >= 0 ? '&' : '?';
url = url + separator + decodeURIComponent(jQuery.param(data));
}
container.text(method + ' ' + getFinalUrl(method, url, data));
};
container.text(method + ' ' + url);
var displayRequestBody = function(method, data, container, header) {
if ('GET' != method && !jQuery.isEmptyObject(data) && data !== "" && data !== undefined) {
if (jQuery.type(data) !== 'string') {
data = decodeURIComponent(jQuery.param(data));
}
container.text(data);
container.show();
header.show();
} else {
container.hide();
header.hide();
}
};
var displayProfilerUrl = function(xhr, link, container) {
Expand All @@ -354,7 +364,7 @@
link.attr('href', '');
container.hide();
}
}
};
var displayResponseData = function(xhr, container) {
var data = xhr.responseText;
Expand All @@ -374,11 +384,51 @@
container.text(text);
};
var displayResponse = function(xhr, method, url, data, result_container) {
var displayCurl = function(method, url, headers, data, result_container) {
var escapeShell = function(param) {
param = "" + param;
return '"' + param.replace(/(["\s'$`\\])/g,'\\$1') + '"';
};
url = getFinalUrl(method, url, data);
var command = "curl";
command += " -X " + escapeShell(method);
if (method != "GET" && !jQuery.isEmptyObject(data) && data !== "" && data !== undefined) {
if (jQuery.type(data) !== 'string') {
data = decodeURIComponent(jQuery.param(data));
}
command += " -d " + escapeShell(data);
}
for (headerKey in headers) {
if (headers.hasOwnProperty(headerKey)) {
command += " -H " + escapeShell(headerKey + ': ' + headers[headerKey]);
}
}
command += " " + url;
result_container.text(command);
};
var getFinalUrl = function(method, url, data) {
if ('GET' == method && !jQuery.isEmptyObject(data)) {
var separator = url.indexOf('?') >= 0 ? '&' : '?';
url = url + separator + decodeURIComponent(jQuery.param(data));
}
return url;
};
var displayResponse = function(xhr, method, url, headers, data, result_container) {
displayFinalUrl(xhr, method, url, data, $('.url', result_container));
displayRequestBody(method, data, $('.request-body', result_container), $('.request-body-header', result_container));
displayProfilerUrl(xhr, $('.profiler-link', result_container), $('.profiler', result_container));
displayResponseData(xhr, $('.response', result_container));
displayResponseHeaders(xhr, $('.headers', result_container));
displayCurl(method, url, headers, data, $('.curl-command', result_container));
result_container.show();
};
Expand Down Expand Up @@ -556,7 +606,7 @@
}
},
complete: function(xhr) {
displayResponse(xhr, method, url, data, result_container);
displayResponse(xhr, method, url, headers, data, result_container);
// and enable them back
$('input:not(.content-type), button', $(self)).removeAttr('disabled');
Expand Down
6 changes: 6 additions & 0 deletions Resources/views/method.html.twig
Expand Up @@ -337,11 +337,17 @@
<h4>Request URL</h4>
<pre class="url"></pre>

<h4 class="request-body-header">Request body</h4>
<pre class="request-body"></pre>

<h4>Response Headers&nbsp;<small>[<a href="" class="to-expand">Expand</a>]</small>&nbsp;<small class="profiler">[<a href="" class="profiler-link" target="_blank">Profiler</a>]</small></h4>
<pre class="headers to-expand"></pre>

<h4>Response Body&nbsp;<small>[<a href="" class="to-raw">Raw</a>]</small></h4>
<pre class="response prettyprint"></pre>

<h4>Curl Command Line</h4>
<pre class="curl-command"></pre>
</div>
{% endif %}
</div>
Expand Down

0 comments on commit aef6a89

Please sign in to comment.