Skip to content

Commit c35e3df

Browse files
author
Emmanouil Konstantinidis
committed
Send Headers
1 parent ecf9396 commit c35e3df

File tree

2 files changed

+34
-6
lines changed
  • rest_framework_docs

2 files changed

+34
-6
lines changed

rest_framework_docs/static/rest_framework_docs/js/index.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var jsonPP = {
2222

2323
$( document ).ready(function() {
2424

25+
var token = null;
26+
2527
var resetForm = function () {
2628
$('#methods').empty();
2729
$('#fields').empty();
@@ -31,8 +33,9 @@ $( document ).ready(function() {
3133
$('#responseStatusCode').removeClass(function (index, css) {
3234
return (css.match (/(^|\s)label-\S+/g) || []).join(' ');
3335
});
34-
$('#responseStatusText').text( '' );
35-
$('#responseData').html( '' );
36+
$('#responseStatusText').text('');
37+
$('#responseData').html('');
38+
$('#saveTokenButton').hide();
3639
};
3740

3841
var setResponse = function (response) {
@@ -63,6 +66,17 @@ $( document ).ready(function() {
6366

6467
$('#responseStatusText').text(response.statusText.toLowerCase());
6568
$('#responseData').html(jsonPP.prettyPrint(response.responseJSON));
69+
70+
// Setup token store
71+
if (response.responseJSON.hasOwnProperty('token')) {
72+
// If the JSON has a token, show the button to save it
73+
$('#saveTokenButton').show();
74+
75+
$('#saveTokenButton').on('click', function () {
76+
// Save the token to the window
77+
token = response.responseJSON['token'];
78+
});
79+
}
6680
};
6781

6882
var getFormData = function () {
@@ -85,9 +99,16 @@ $( document ).ready(function() {
8599
var url = $('#requestForm #urlInput').val();
86100
var method = $("#methods").find( ".active" ).text();
87101
var data = getFormData();
102+
103+
var token = $('#headers #authorization').val();
104+
var headers = token ? {
105+
'Authorization' : token
106+
} : null;
107+
88108
$.ajax({
89109
url: url,
90110
method: method,
111+
headers: headers,
91112
context: document.body,
92113
data: data
93114
}).always(function(data, textStatus, jqXHR) {
@@ -120,11 +141,14 @@ $( document ).ready(function() {
120141
};
121142

122143
var _setupAuthorization = function (permissions) {
123-
124-
console.log(permissions);
125144
if (permissions === 'None' || permissions === 'AllowAny') {
126-
console.log('EMPTYYYY');
127-
$('#headers').empty();
145+
$('#headers').hide();
146+
} else {
147+
$('#headers').show();
148+
// Check if token exists
149+
if (token) {
150+
$('#headers #authorization').val('Token ' + token);
151+
}
128152
}
129153
};
130154

@@ -145,6 +169,7 @@ $( document ).ready(function() {
145169
var setupForm = function (data) {
146170
// Reset Form - Remove Methods & Fields
147171
resetForm();
172+
cleanResponse();
148173

149174
$('#urlInput').val(data.path);
150175

rest_framework_docs/templates/rest_framework_docs/home.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ <h5 class="section-title"><span>Data</span></h5>
132132
<h3>Response <span id="responseStatusCode" class="label status-code pull-right"></span></h3>
133133
<div><strong>Status Text</strong>: <span class="status-text" id="responseStatusText"></span></div>
134134
<pre><code id="responseData"></code></pre>
135+
<div id="saveTokenButton">
136+
<button class='btn btn-info'><i class='fa fa-key'></i> Save Token</button>
137+
</div>
135138
</div>
136139
</div>
137140

0 commit comments

Comments
 (0)