@@ -57,8 +57,8 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
5757 // content[k] = v
5858 // })
5959 let serial = NSJSONSerialization . dataWithJSONObjectOptionsError ( data , NSJSONWritingOptions . PrettyPrinted )
60- let results = NSString . alloc ( ) . initWithDataEncoding ( serial , NSUTF8StringEncoding )
61- content = JSON . parse ( results . toString ( ) )
60+ content = NSString . alloc ( ) . initWithDataEncoding ( serial , NSUTF8StringEncoding ) . toString ( )
61+ // console.log('content', content )
6262 } else if ( data . class ( ) . name == 'NSData' ) {
6363 content = NSString . alloc ( ) . initWithDataEncoding ( data , NSASCIIStringEncoding ) . toString ( )
6464 // } else if (data.class().name == 'NSArray') {
@@ -80,9 +80,15 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
8080 } else {
8181 content = data
8282 }
83+
84+ try {
85+ content = JSON . parse ( content )
86+ } catch ( e ) { }
87+
8388 } else {
8489 content = data
8590 }
91+
8692 resolve ( { task, content } )
8793}
8894
@@ -92,9 +98,12 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError)
9298 // console.log('error.userInfo.description', error.userInfo.description)
9399 // console.log('error.localizedDescription', error.localizedDescription)
94100 let data : NSData = error . userInfo . valueForKey ( AFNetworkingOperationFailingURLResponseDataErrorKey )
95- let body = NSString . alloc ( ) . initWithDataEncoding ( data , NSUTF8StringEncoding )
101+ let body = NSString . alloc ( ) . initWithDataEncoding ( data , NSUTF8StringEncoding ) . toString ( )
102+ try {
103+ body = JSON . parse ( body )
104+ } catch ( e ) { }
96105 let content : any = {
97- body : body . description ,
106+ body,
98107 description : error . description ,
99108 reason : error . localizedDescription ,
100109 url : error . userInfo . objectForKey ( 'NSErrorFailingURLKey' ) . description
@@ -134,7 +143,7 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
134143
135144 let manager = AFHTTPSessionManager . manager ( )
136145
137- if ( opts . headers [ 'Content-Type' ] == 'application/json' ) {
146+ if ( opts . headers && opts . headers [ 'Content-Type' ] == 'application/json' ) {
138147 manager . requestSerializer = AFJSONRequestSerializer . serializer ( )
139148 manager . responseSerializer = AFJSONResponseSerializer . serializerWithReadingOptions ( NSJSONReadingOptions . AllowFragments )
140149 } else {
@@ -156,8 +165,8 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
156165 }
157166
158167 let dict : NSMutableDictionary < string , any > = null
159- if ( opts . content ) {
160- let cont = JSON . parse ( opts . content as any )
168+ if ( opts . body ) {
169+ let cont = opts . body
161170 if ( isObject ( cont ) ) {
162171 dict = NSMutableDictionary . new < string , any > ( )
163172 Object . keys ( cont ) . forEach ( function ( key ) {
@@ -166,19 +175,33 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
166175 }
167176 }
168177
169- if ( opts . method == 'GET' ) {
170- manager . GETParametersSuccessFailure ( opts . url , dict , function success ( task : NSURLSessionDataTask , data : any ) {
171- AFSuccess ( resolve , task , data )
172- } , function failure ( task , error ) {
173- AFFailure ( resolve , reject , task , error )
174- } )
175- } else if ( opts . method == 'POST' ) {
176- manager . POSTParametersSuccessFailure ( opts . url , dict , function success ( task : NSURLSessionDataTask , data : any ) {
177- AFSuccess ( resolve , task , data )
178- } , function failure ( task , error ) {
179- AFFailure ( resolve , reject , task , error )
180- } )
178+ let methods = {
179+ 'GET' : 'GETParametersSuccessFailure' ,
180+ 'POST' : 'POSTParametersSuccessFailure' ,
181+ 'PUT' : 'PUTParametersSuccessFailure' ,
182+ 'DELETE' : 'DELETEParametersSuccessFailure' ,
183+ 'PATCH' : 'PATCHParametersSuccessFailure' ,
184+ 'HEAD' : 'HEADParametersSuccessFailure' ,
181185 }
186+ manager [ methods [ opts . method ] ] ( opts . url , dict , function success ( task : NSURLSessionDataTask , data : any ) {
187+ AFSuccess ( resolve , task , data )
188+ } , function failure ( task , error ) {
189+ AFFailure ( resolve , reject , task , error )
190+ } )
191+
192+ // if (opts.method == 'GET') {
193+ // manager.GETParametersSuccessFailure(opts.url, dict, function success(task: NSURLSessionDataTask, data: any) {
194+ // AFSuccess(resolve, task, data)
195+ // }, function failure(task, error) {
196+ // AFFailure(resolve, reject, task, error)
197+ // })
198+ // } else if (opts.method == 'POST') {
199+ // manager.POSTParametersSuccessFailure(opts.url, dict, function success(task: NSURLSessionDataTask, data: any) {
200+ // AFSuccess(resolve, task, data)
201+ // }, function failure(task, error) {
202+ // AFFailure(resolve, reject, task, error)
203+ // })
204+ // }
182205
183206 } catch ( error ) {
184207 reject ( error )
0 commit comments