@@ -119,6 +119,7 @@ export default class ApiRequest extends LitElement {
119
119
this . responseHeaders = '' ;
120
120
this . responseText = '' ;
121
121
this . responseUrl = '' ;
122
+ this . curlSyntax = '' ;
122
123
123
124
}
124
125
@@ -198,8 +199,8 @@ export default class ApiRequest extends LitElement {
198
199
if ( mimeReq . includes ( 'json' ) ) { shortMimeTypes [ mimeReq ] = 'json' ; }
199
200
else if ( mimeReq . includes ( 'xml' ) ) { shortMimeTypes [ mimeReq ] = 'xml' ; }
200
201
else if ( mimeReq . includes ( 'text/plain' ) ) { shortMimeTypes [ mimeReq ] = 'text' ; }
201
- else if ( mimeReq . includes ( 'form-urlencoded' ) ) { shortMimeTypes [ mimeReq ] = 'form' ; }
202
- else if ( mimeReq . includes ( 'multipart- form' ) ) { shortMimeTypes [ mimeReq ] = 'multipart-form' ; }
202
+ else if ( mimeReq . includes ( 'form-urlencoded' ) ) { shortMimeTypes [ mimeReq ] = 'form-urlencoded ' ; }
203
+ else if ( mimeReq . includes ( 'multipart/ form-data ' ) ) { shortMimeTypes [ mimeReq ] = 'multipart-form-data ' ; }
203
204
204
205
let mimeReqObj = content [ mimeReq ] ;
205
206
let reqExample = "" ;
@@ -320,12 +321,16 @@ export default class ApiRequest extends LitElement {
320
321
< div id ="tab_buttons " class ="tab-buttons row " @click ="${ this . activateTab } ">
321
322
< button class ="tab-btn active " content_id ="tab_response_text "> RESPONSE TEXT</ button >
322
323
< button class ="tab-btn " content_id ="tab_response_headers "> RESPONSE HEADERS</ button >
324
+ < button class ="tab-btn " content_id ="tab_curl "> CURL</ button >
323
325
</ div >
324
326
< div id ="tab_response_text " class ="tab-content col " style ="flex:1; ">
325
- < textarea class ="mono " style ="min-height:180px; padding:16px; white-space:nowrap; "> ${ this . responseText } </ textarea >
327
+ < textarea class ="mono " style ="min-height:180px; padding:16px; white-space:nowrap; "> ${ this . responseText } </ textarea >
326
328
</ div >
327
329
< div id ="tab_response_headers " class ="tab-content col " style ="flex:1;display:none ">
328
- < textarea class ="mono " style ="min-height:180px; padding:16px; white-space:nowrap; "> ${ this . responseHeaders } </ textarea >
330
+ < textarea class ="mono " style ="min-height:180px; padding:16px; white-space:nowrap; "> ${ this . responseHeaders } </ textarea >
331
+ </ div >
332
+ < div id ="tab_curl " class ="tab-content col " style ="flex:1;display:none ">
333
+ < code style ="min-height:180px; padding:16px;font-size:12px; border:1px solid var(--input-border-color);overflow: scroll;word-break: break-word; "> ${ this . curlSyntax } </ code >
329
334
</ div >
330
335
</ div > ` }
331
336
`
@@ -361,6 +366,7 @@ export default class ApiRequest extends LitElement {
361
366
362
367
onTryClick ( e ) {
363
368
let me = this ;
369
+ let curl = "" , curlHeaders = "" , curlData = "" , curlForm = "" ;
364
370
let requestPanelEl = e . target . closest ( ".request-panel" ) ;
365
371
let pathParamEls = [ ...requestPanelEl . querySelectorAll ( ".request-param[data-ptype='path']" ) ] ;
366
372
let queryParamEls = [ ...requestPanelEl . querySelectorAll ( ".request-param[data-ptype='query']" ) ] ;
@@ -374,7 +380,7 @@ export default class ApiRequest extends LitElement {
374
380
'method' : this . method . toUpperCase ( ) ,
375
381
'headers' :{ } ,
376
382
}
377
-
383
+
378
384
//Path Params
379
385
pathParamEls . map ( function ( el ) {
380
386
fetchUrl = fetchUrl . replace ( "{" + el . dataset . pname + "}" , el . value ) ;
@@ -393,66 +399,86 @@ export default class ApiRequest extends LitElement {
393
399
if ( this . apiKeyValue && this . apiKeyName && this . apiKeyLocation === 'query' ) {
394
400
fetchUrl = `${ fetchUrl } &${ this . apiKeyName } =${ this . apiKeyValue } ` ;
395
401
}
402
+
403
+ //Final URL
396
404
fetchUrl = `${ this . server . replace ( / \/ $ / , "" ) } ${ fetchUrl } ` ;
405
+ curl = `curl -X ${ this . method . toUpperCase ( ) } "${ fetchUrl } " ` ;
397
406
398
407
//Header Params
399
408
headerParamEls . map ( function ( el ) {
400
409
if ( el . value ) {
401
410
fetchOptions . headers [ el . dataset . pname ] = el . value ;
411
+ curlHeaders = curlHeaders + ` -H "${ fetchOptions . headers [ el . dataset . pname ] } : ${ el . value } "` ;
402
412
}
403
413
} ) ;
404
414
// Add Authentication Header if provided
405
415
if ( this . apiKeyValue && this . apiKeyName && this . apiKeyLocation === 'header' ) {
406
416
fetchOptions . headers [ this . apiKeyName ] = this . apiKeyValue ;
417
+ curlHeaders = curlHeaders + ` -H "${ this . apiKeyName } : ${ this . apiKeyValue } "` ;
407
418
}
408
419
409
420
//Form Params
410
421
if ( formParamEls . length >= 1 ) {
411
422
let formEl = requestPanelEl . querySelector ( "form" ) ;
412
- fetchOptions . body = new FormData ( formEl ) ;
413
423
if ( formEl . classList . contains ( "form-urlencoded" ) ) {
424
+ let formUrlParams = new URLSearchParams ( ) ;
414
425
fetchOptions . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded; charset=utf-8'
426
+ curlHeaders = curlHeaders + ` -H "Content-Type: application/x-www-form-urlencoded"` ;
427
+ formParamEls . map ( function ( el ) {
428
+ if ( el . value ) {
429
+ formUrlParams . append ( el . dataset . pname , el . value ) ;
430
+ curlForm = curlForm + ` -F "${ el . dataset . pname } =${ el . value } "` ;
431
+ }
432
+ } ) ;
433
+ fetchOptions . body = formUrlParams ;
415
434
}
416
435
else {
417
- fetchOptions . headers [ 'Content-Type' ] = 'multipart/form-data; charset=utf-8'
436
+ //fetchOptions.headers['Content-Type'] = 'multipart/form-data; charset=utf-8'
437
+ fetchOptions . body = new FormData ( formEl ) ;
418
438
}
419
439
}
420
440
421
441
//Body Params (json/xml/text)
422
442
if ( bodyParamEls . length >= 1 ) {
423
443
if ( bodyParamEls . length === 1 ) {
424
444
fetchOptions . headers [ 'Content-Type' ] = bodyParamEls [ 0 ] . dataset . ptype ;
445
+ curlHeaders = curlHeaders + ` -H "Content-Type: ${ bodyParamEls [ 0 ] . dataset . ptype } "` ;
425
446
fetchOptions . body = bodyParamEls [ 0 ] . value ;
447
+ curlData = ` -d ${ JSON . stringify ( bodyParamEls [ 0 ] . value . replace ( / ( \r \n | \n | \r ) / gm, "" ) ) } ` ;
426
448
}
427
449
else {
428
450
let mimeTypeRadioEl = e . target . closest ( ".request-panel" ) . querySelector ( "input[name='request_body_type']:checked" ) ;
429
451
let selectedBody = mimeTypeRadioEl === null ?'json' :mimeTypeRadioEl . value ;
430
452
let bodyData = '' ;
431
453
if ( selectedBody === 'json' ) {
432
454
bodyData = requestPanelEl . querySelector ( ".request-body-param.json" ) . value ;
433
- fetchOptions . headers [ 'Content-Type' ] = 'application/json; charset=utf-8'
434
- fetchOptions . body = bodyData ;
455
+ fetchOptions . headers [ 'Content-Type' ] = 'application/json; charset=utf-8' ;
456
+ curlHeaders = curlHeaders + ` -H "Content-Type: application/json"` ;
435
457
}
436
458
else if ( selectedBody === 'xml' ) {
437
459
bodyData = requestPanelEl . querySelector ( ".request-body-param.xml" ) . value ;
438
- fetchOptions . headers [ 'Content-Type' ] = 'application/xml; charset=utf-8'
439
- fetchOptions . body = bodyData ;
460
+ fetchOptions . headers [ 'Content-Type' ] = 'application/xml; charset=utf-8' ;
461
+ curlHeaders = curlHeaders + ` -H "Content-Type: application/xml"` ;
440
462
}
441
463
else if ( selectedBody === 'text' ) {
442
464
bodyData = requestPanelEl . querySelector ( ".request-body-param.text" ) . value ;
443
- fetchOptions . headers [ 'Content-Type' ] = 'text/plain; charset=utf-8'
444
- fetchOptions . body = bodyData ;
465
+ fetchOptions . headers [ 'Content-Type' ] = 'text/plain; charset=utf-8' ;
466
+ curlHeaders = curlHeaders + ` -H "Content-Type: text/plain"` ;
445
467
}
468
+ fetchOptions . body = bodyData ;
469
+ curlData = ` -d ${ JSON . stringify ( bodyData . replace ( / ( \r \n | \n | \r ) / gm, "" ) ) } ` ;
446
470
}
447
471
}
448
472
449
473
me . responseUrl = '' ;
450
474
me . responseHeaders = '' ;
451
475
me . responseText = '' ;
476
+ me . curlSyntax = '' ;
452
477
me . responseStatus = 'success' ;
453
478
me . responseMessage = ''
454
479
455
480
fetch ( fetchUrl , fetchOptions ) . then ( function ( resp ) {
481
+ me . curlSyntax = `${ curl } ${ curlHeaders } ${ curlData } ${ curlForm } ` ;
456
482
me . responseStatus = resp . ok ? 'success' :'error' ;
457
483
me . responseMessage = `${ resp . statusText } :${ resp . status } ` ;
458
484
me . responseUrl = resp . url ;
0 commit comments