@@ -58,7 +58,11 @@ let _cookiesEnabled = true;
5858
5959class HttpsResponseLegacy implements IHttpsResponseLegacy {
6060 private callback ?: com . nativescript . https . OkHttpResponse . OkHttpResponseAsyncCallback ;
61- constructor ( private response : com . nativescript . https . OkHttpResponse , private tag : string , private url : string ) { }
61+ constructor (
62+ private response : com . nativescript . https . OkHttpResponse ,
63+ private tag : string ,
64+ private url : string
65+ ) { }
6266
6367 get contentLength ( ) {
6468 return this . response . contentLength ( ) ;
@@ -495,70 +499,70 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
495499 // if (['GET', 'HEAD'].indexOf(opts.method) !== -1 || (opts.method === 'DELETE' && !Utils.isDefined(opts.body) && !Utils.isDefined(opts.content))) {
496500 // request[methods[opts.method]]();
497501 // } else {
498- let type = opts . headers && opts . headers [ 'Content-Type' ] ? opts . headers [ 'Content-Type' ] : 'application/json' ;
499- const MEDIA_TYPE = okhttp3 . MediaType . parse ( type ) ;
500- let okHttpBody : okhttp3 . RequestBody ;
501- if ( type . startsWith ( 'multipart/form-data' ) ) {
502- const builder = new okhttp3 . MultipartBody . Builder ( ) ;
503- builder . setType ( MEDIA_TYPE ) ;
504-
505- ( opts . body as HttpsFormDataParam [ ] ) . forEach ( ( param ) => {
506- if ( param . fileName && param . contentType ) {
507- if ( param . data instanceof okhttp3 . RequestBody ) {
508- builder . addFormDataPart ( param . parameterName , param . fileName , param . data ) ;
509- } else {
510- builder . addFormDataPart ( param . parameterName , param . fileName , okhttp3 . RequestBody . create ( param . data , okhttp3 . MediaType . parse ( param . contentType ) ) ) ;
511- }
502+ const type = opts . headers && opts . headers [ 'Content-Type' ] ? opts . headers [ 'Content-Type' ] : 'application/json' ;
503+ const MEDIA_TYPE = okhttp3 . MediaType . parse ( type ) ;
504+ let okHttpBody : okhttp3 . RequestBody ;
505+ if ( type . startsWith ( 'multipart/form-data' ) ) {
506+ const builder = new okhttp3 . MultipartBody . Builder ( ) ;
507+ builder . setType ( MEDIA_TYPE ) ;
508+
509+ ( opts . body as HttpsFormDataParam [ ] ) . forEach ( ( param ) => {
510+ if ( param . fileName && param . contentType ) {
511+ if ( param . data instanceof okhttp3 . RequestBody ) {
512+ builder . addFormDataPart ( param . parameterName , param . fileName , param . data ) ;
512513 } else {
513- if ( typeof param . data === 'string' ) {
514- builder . addFormDataPart ( param . parameterName , param . data ) ;
515- } else {
516- builder . addFormDataPart ( param . parameterName , param . data + '' ) ;
517- }
514+ builder . addFormDataPart ( param . parameterName , param . fileName , okhttp3 . RequestBody . create ( param . data , okhttp3 . MediaType . parse ( param . contentType ) ) ) ;
518515 }
519- } ) ;
520- okHttpBody = builder . build ( ) ;
521- } else if ( type === 'application/x-www-form-urlencoded' ) {
522- const builder = new okhttp3 . FormBody . Builder ( ) ;
523- Object . keys ( opts . body ) . forEach ( ( key ) => {
524- builder . add ( key , opts . body [ key ] ) ;
525- } ) ;
526- okHttpBody = builder . build ( ) ;
527- } else {
528- let body ;
529- if ( opts . body ) {
530- // TODO: add support for Buffers
531- if ( opts . body instanceof File ) {
532- okHttpBody = okhttp3 . RequestBody . create ( new java . io . File ( opts . body . path ) , okhttp3 . MediaType . parse ( type ) ) ;
533- } else if ( typeof opts . body === 'string' ) {
534- body = opts . body ;
535- } else {
536- try {
537- body = JSON . stringify ( opts . body ) ;
538- } catch ( ignore ) { }
516+ } else {
517+ if ( typeof param . data === 'string' ) {
518+ builder . addFormDataPart ( param . parameterName , param . data ) ;
519+ } else {
520+ builder . addFormDataPart ( param . parameterName , param . data + '' ) ;
539521 }
540- } else if ( opts . content ) {
541- body = opts . content ;
542522 }
543- if ( body instanceof okhttp3 . RequestBody ) {
544- okHttpBody = body ;
545- } else if ( body ) {
546- okHttpBody = okhttp3 . RequestBody . create ( body , okhttp3 . MediaType . parse ( type ) ) ;
523+ } ) ;
524+ okHttpBody = builder . build ( ) ;
525+ } else if ( type === 'application/x-www-form-urlencoded' ) {
526+ const builder = new okhttp3 . FormBody . Builder ( ) ;
527+ Object . keys ( opts . body ) . forEach ( ( key ) => {
528+ builder . add ( key , opts . body [ key ] ) ;
529+ } ) ;
530+ okHttpBody = builder . build ( ) ;
531+ } else {
532+ let body ;
533+ if ( opts . body ) {
534+ // TODO: add support for Buffers
535+ if ( opts . body instanceof File ) {
536+ okHttpBody = okhttp3 . RequestBody . create ( new java . io . File ( opts . body . path ) , okhttp3 . MediaType . parse ( type ) ) ;
537+ } else if ( typeof opts . body === 'string' ) {
538+ body = opts . body ;
539+ } else {
540+ try {
541+ body = JSON . stringify ( opts . body ) ;
542+ } catch ( ignore ) { }
547543 }
544+ } else if ( opts . content ) {
545+ body = opts . content ;
548546 }
549-
550- if ( okHttpBody && opts . onProgress ) {
551- okHttpBody = new com . nativescript . https . ProgressRequestWrapper (
552- okHttpBody ,
553- new com . nativescript . https . ProgressRequestWrapper . ProgressListener ( {
554- onRequestProgress ( bytesWritten : number , contentLength : number ) {
555- opts . onProgress ( bytesWritten , contentLength ) ;
556- }
557- } )
558- ) ;
547+ if ( body instanceof okhttp3 . RequestBody ) {
548+ okHttpBody = body ;
549+ } else if ( body ) {
550+ okHttpBody = okhttp3 . RequestBody . create ( body , okhttp3 . MediaType . parse ( type ) ) ;
559551 }
560- request . method ( opts . method , okHttpBody ) ;
561- // request[methods[opts.method]](okHttpBody);
552+ }
553+
554+ if ( okHttpBody && opts . onProgress ) {
555+ okHttpBody = new com . nativescript . https . ProgressRequestWrapper (
556+ okHttpBody ,
557+ new com . nativescript . https . ProgressRequestWrapper . ProgressListener ( {
558+ onRequestProgress ( bytesWritten : number , contentLength : number ) {
559+ opts . onProgress ( bytesWritten , contentLength ) ;
560+ }
561+ } )
562+ ) ;
563+ }
564+ request . method ( opts . method , okHttpBody ) ;
565+ // request[methods[opts.method]](okHttpBody);
562566 // }
563567 const tag = opts . tag || `okhttp_request_${ CALL_ID ++ } ` ;
564568 const call = client . newCall ( request . tag ( tag ) . build ( ) ) ;
0 commit comments