Browse Source

Add response info to HTTP request so that JS context can get response headers and status

Ben Hsieh 8 years ago
parent
commit
72dfbf0c06

+ 43
- 3
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

12
 
12
 
13
 import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
13
 import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
14
 import com.RNFetchBlob.Response.RNFetchBlobFileResp;
14
 import com.RNFetchBlob.Response.RNFetchBlobFileResp;
15
+import com.facebook.react.bridge.Arguments;
15
 import com.facebook.react.bridge.Callback;
16
 import com.facebook.react.bridge.Callback;
16
 import com.facebook.react.bridge.ReactApplicationContext;
17
 import com.facebook.react.bridge.ReactApplicationContext;
17
 import com.facebook.react.bridge.ReadableArray;
18
 import com.facebook.react.bridge.ReadableArray;
18
 import com.facebook.react.bridge.ReadableMap;
19
 import com.facebook.react.bridge.ReadableMap;
19
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
20
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
21
+import com.facebook.react.bridge.WritableMap;
20
 
22
 
21
 import java.io.ByteArrayInputStream;
23
 import java.io.ByteArrayInputStream;
22
 import java.io.File;
24
 import java.io.File;
28
 import java.util.HashMap;
30
 import java.util.HashMap;
29
 
31
 
30
 import okhttp3.Call;
32
 import okhttp3.Call;
33
+import okhttp3.Headers;
31
 import okhttp3.Interceptor;
34
 import okhttp3.Interceptor;
32
 import okhttp3.MediaType;
35
 import okhttp3.MediaType;
33
 import okhttp3.OkHttpClient;
36
 import okhttp3.OkHttpClient;
36
 import okhttp3.Response;
39
 import okhttp3.Response;
37
 import okhttp3.ResponseBody;
40
 import okhttp3.ResponseBody;
38
 import okhttp3.FormBody;
41
 import okhttp3.FormBody;
42
+import okhttp3.internal.framed.Header;
39
 
43
 
40
 /**
44
 /**
41
  * Created by wkh237 on 2016/6/21.
45
  * Created by wkh237 on 2016/6/21.
308
             case KeepInMemory:
312
             case KeepInMemory:
309
                 try {
313
                 try {
310
                     byte [] b = resp.body().bytes();
314
                     byte [] b = resp.body().bytes();
311
-                    callback.invoke(null, android.util.Base64.encodeToString(b,Base64.NO_WRAP));
315
+                    callback.invoke(null, getResponseInfo(resp), android.util.Base64.encodeToString(b,Base64.NO_WRAP));
312
                 } catch (IOException e) {
316
                 } catch (IOException e) {
313
                     callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);
317
                     callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);
314
                 }
318
                 }
319
                 } catch (Exception ignored) {
323
                 } catch (Exception ignored) {
320
 
324
 
321
                 }
325
                 }
322
-                callback.invoke(null, this.destPath);
326
+                callback.invoke(null, getResponseInfo(resp), this.destPath);
323
                 break;
327
                 break;
324
             default:
328
             default:
325
                 try {
329
                 try {
326
-                    callback.invoke(null, new String(resp.body().bytes(), "UTF-8"));
330
+                    callback.invoke(null, getResponseInfo(resp), new String(resp.body().bytes(), "UTF-8"));
327
                 } catch (IOException e) {
331
                 } catch (IOException e) {
328
                     callback.invoke("RNFetchBlob failed to encode response data to UTF8 string.", null);
332
                     callback.invoke("RNFetchBlob failed to encode response data to UTF8 string.", null);
329
                 }
333
                 }
333
             taskTable.remove(taskId);
337
             taskTable.remove(taskId);
334
     }
338
     }
335
 
339
 
340
+    private WritableMap getResponseInfo(Response resp) {
341
+        WritableMap info = Arguments.createMap();
342
+        info.putInt("status", resp.code());
343
+        info.putString("state", "2");
344
+        info.putString("taskId", this.taskId);
345
+        WritableMap headers = Arguments.createMap();
346
+        for(int i =0;i< resp.headers().size();i++) {
347
+            headers.putString(resp.headers().name(i), resp.headers().value(i));
348
+        }
349
+        info.putMap("headers", headers);
350
+        Headers h = resp.headers();
351
+        if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("text/plain"))
352
+        {
353
+            info.putString("respType", "text");
354
+        }
355
+        else if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("application/json"))
356
+        {
357
+            info.putString("respType", "json");
358
+        }
359
+        else if(getHeaderIgnoreCases(h, "content-type").length() < 1)
360
+        {
361
+            info.putString("respType", "blob");
362
+        }
363
+        else
364
+        {
365
+            info.putString("respType", "text");
366
+        }
367
+        return info;
368
+    }
369
+
370
+    private String getHeaderIgnoreCases(Headers headers, String field) {
371
+        String val = headers.get(field);
372
+        if(val != null) return val;
373
+        return headers.get(field.toLowerCase()) == null ? "" : headers.get(field.toLowerCase());
374
+    }
375
+
336
     /**
376
     /**
337
      * Build request body by given string
377
      * Build request body by given string
338
      * @param body Content of request body in UTF8 string format.
378
      * @param body Content of request body in UTF8 string format.

+ 22
- 9
src/ios/RNFetchBlobNetwork.m View File

30
     NSString * destPath;
30
     NSString * destPath;
31
     NSOutputStream * writeStream;
31
     NSOutputStream * writeStream;
32
     long bodyLength;
32
     long bodyLength;
33
+    NSMutableDictionary * respInfo;
33
 }
34
 }
34
 
35
 
35
 @end
36
 @end
134
 
135
 
135
         if(path != nil)
136
         if(path != nil)
136
             destPath = path;
137
             destPath = path;
138
+        else
139
+            destPath = [RNFetchBlobFS getTempPath:cacheKey withExtension:[self.options valueForKey:CONFIG_FILE_EXT]];
137
     }
140
     }
138
     else
141
     else
139
     {
142
     {
186
         }
189
         }
187
         else
190
         else
188
             respType = @"";
191
             respType = @"";
192
+        respInfo = @{
193
+                     @"taskId": taskId,
194
+                     @"state": @"2",
195
+                     @"headers": headers,
196
+                     @"respType" : respType,
197
+                     @"status": [NSString stringWithFormat:@"%d", statusCode ]
198
+                     };
189
         [self.bridge.eventDispatcher
199
         [self.bridge.eventDispatcher
190
          sendDeviceEventWithName: EVENT_STATE_CHANGE
200
          sendDeviceEventWithName: EVENT_STATE_CHANGE
191
-         body:@{
192
-                @"taskId": taskId,
193
-                @"state": @"2",
194
-                @"headers": headers,
195
-                @"respType" : respType,
196
-                @"status": [NSString stringWithFormat:@"%d", statusCode ]
197
-            }
201
+         body:respInfo
198
          ];
202
          ];
199
     }
203
     }
200
 
204
 
241
     NSLog([error localizedDescription]);
245
     NSLog([error localizedDescription]);
242
     self.error = error;
246
     self.error = error;
243
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
247
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
248
+    
249
+    NSString * respType = [respInfo valueForKey:@"respType"];
250
+    
244
     if(respFile == YES)
251
     if(respFile == YES)
245
     {
252
     {
246
         [writeStream close];
253
         [writeStream close];
247
-        callback(@[error == nil ? [NSNull null] : [error localizedDescription], destPath]);
254
+        callback(@[error == nil ? [NSNull null] : [error localizedDescription],
255
+                   respInfo == nil ? [NSNull null] : respInfo,
256
+                   destPath
257
+                   ]);
248
     }
258
     }
249
     // base64 response
259
     // base64 response
250
     else {
260
     else {
251
         NSString * res = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
261
         NSString * res = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
252
-        callback(@[error == nil ? [NSNull null] : [error localizedDescription], [respData base64EncodedStringWithOptions:0]]);
262
+        callback(@[error == nil ? [NSNull null] : [error localizedDescription],
263
+                   respInfo == nil ? [NSNull null] : respInfo,
264
+                   [respData base64EncodedStringWithOptions:0]
265
+                   ]);
253
     }
266
     }
254
 }
267
 }
255
 
268