@@ -115,8 +115,11 @@ function bodyToNative(cont) {
115115export function request ( opts : Https . HttpsRequestOptions ) : Promise < Https . HttpsResponse > {
116116 return new Promise ( ( resolve , reject ) => {
117117 try {
118- const manager = AFHTTPSessionManager . alloc ( ) . initWithBaseURL ( NSURL . URLWithString ( opts . url ) ) ;
119- if ( opts . headers && ( < any > opts . headers [ 'Content-Type' ] ) . substring ( 0 , 16 ) === 'application/json' ) {
118+ const manager = AFHTTPSessionManager . alloc ( ) . initWithBaseURL (
119+ NSURL . URLWithString ( opts . url )
120+ ) ;
121+ const type = opts . headers && opts . headers [ 'Content-Type' ] ? < string > opts . headers [ 'Content-Type' ] : 'application/json' ;
122+ if ( type === "application/json" ) {
120123 manager . requestSerializer = AFJSONRequestSerializer . serializer ( ) ;
121124 manager . responseSerializer = AFJSONResponseSerializer . serializerWithReadingOptions ( NSJSONReadingOptions . AllowFragments ) ;
122125 } else {
@@ -127,7 +130,6 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
127130 manager . securityPolicy = ( policies . secured === true ) ? policies . secure : policies . def ;
128131
129132 if ( opts . cachePolicy ) {
130- let cacheControlBuilder = new okhttp3 . CacheControl . Builder ( ) ;
131133 switch ( opts . cachePolicy ) {
132134 case "noCache" :
133135 manager . setDataTaskWillCacheResponseBlock ( ( session , task , cacheResponse ) => {
@@ -158,23 +160,11 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
158160
159161 manager . requestSerializer . timeoutInterval = opts . timeout ? opts . timeout : 10 ;
160162
161- let methods = {
162- GET : "GETParametersSuccessFailure" ,
163- POST : "POSTParametersSuccessFailure" ,
164- PUT : "PUTParametersSuccessFailure" ,
165- DELETE : "DELETEParametersSuccessFailure" ,
166- PATCH : "PATCHParametersSuccessFailure" ,
167- HEAD : "HEADParametersSuccessFailure" ,
168- } ;
169- manager [ methods [ opts . method ] ] (
170- opts . url ,
171- dict ,
172- function success ( task : NSURLSessionDataTask , data : any ) {
173- AFSuccess ( resolve , task , data ) ;
174- } ;
175-
176- const failure = ( task , error ) => {
177- AFFailure ( resolve , reject , task , error ) ;
163+ const success = function ( task : NSURLSessionDataTask , data ?: any ) {
164+ AFSuccess ( resolve , task , data ) ;
165+ }
166+ const failure = function ( task : NSURLSessionDataTask , error : any ) {
167+ AFFailure ( resolve , reject , task , error ) ;
178168 } ;
179169
180170 const progress = ( progress : NSProgress ) => {
@@ -193,9 +183,49 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
193183 manager . PATCHParametersHeadersSuccessFailure ( opts . url , dict , headers , success , failure ) ;
194184 } else if ( opts . method === "HEAD" ) {
195185 manager . HEADParametersHeadersSuccessFailure ( opts . url , dict , headers , success , failure ) ;
186+ }
187+ if ( type === "application/json" ) {
188+ switch ( opts . method ) {
189+ case 'POST' :
190+ manager . POSTParametersConstructingBodyWithBlockSuccessFailure ( opts . url , null , ( formData ) => {
191+ Object . keys ( opts . body ) . forEach ( k => {
192+ const param = opts . body [ k ] as Https . HttpsFormDataParam ;
193+ if ( param . fileName && param . contentType ) {
194+ formData . appendPartWithFileDataNameFileNameMimeType ( param . data , param . parameterName , param . fileName , param . contentType ) ;
195+ } else {
196+ formData . appendPartWithFormDataName ( NSString . stringWithString ( param . data ) . dataUsingEncoding ( NSUTF8StringEncoding ) , param . parameterName )
197+ }
198+ } )
199+ } , success , failure ) ;
200+ break ;
201+ default :
202+ reject ( new Error ( 'method_not_supported_multipart' ) ) ;
203+ }
204+ } else {
205+ switch ( opts . method ) {
206+ case 'GET' :
207+ manager . GETParametersSuccessFailure ( opts . url , dict , success , failure ) ;
208+ break ;
209+ case 'POST' :
210+ manager . POSTParametersSuccessFailure ( opts . url , dict , success , failure ) ;
211+ break ;
212+ case 'PUT' :
213+ manager . PUTParametersSuccessFailure ( opts . url , dict , success , failure ) ;
214+ break ;
215+ case 'DELETE' :
216+ manager . DELETEParametersSuccessFailure ( opts . url , dict , success , failure ) ;
217+ break ;
218+ case 'PATCH' :
219+ manager . PATCHParametersSuccessFailure ( opts . url , dict , success , failure ) ;
220+ break ;
221+ case 'HEAD' :
222+ manager . HEADParametersSuccessFailure ( opts . url , dict , success , failure ) ;
223+ break ;
224+ default :
225+ reject ( new Error ( 'method_not_supported_multipart' ) ) ;
196226 }
197-
198-
227+ }
228+
199229 } catch ( error ) {
200230 reject ( error ) ;
201231 }
0 commit comments