@@ -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 ) l a b e l - \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
0 commit comments