|
@@ -82,6 +82,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
82
|
82
|
RequestType requestType;
|
83
|
83
|
ResponseType responseType;
|
84
|
84
|
boolean timeout = false;
|
|
85
|
+ WritableMap respInfo;
|
85
|
86
|
|
86
|
87
|
public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
|
87
|
88
|
this.method = method;
|
|
@@ -250,7 +251,12 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
250
|
251
|
break;
|
251
|
252
|
|
252
|
253
|
case WithoutBody:
|
253
|
|
- builder.method(method, null);
|
|
254
|
+ if(method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("PUT"))
|
|
255
|
+ {
|
|
256
|
+ builder.method(method, RequestBody.create(null, new byte[0]));
|
|
257
|
+ }
|
|
258
|
+ else
|
|
259
|
+ builder.method(method, null);
|
254
|
260
|
break;
|
255
|
261
|
}
|
256
|
262
|
|
|
@@ -291,23 +297,36 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
291
|
297
|
return chain.proceed(chain.request());
|
292
|
298
|
}
|
293
|
299
|
});
|
294
|
|
-
|
295
|
|
- if(options.timeout != -1) {
|
|
300
|
+
|
|
301
|
+ if(options.timeout > 0) {
|
296
|
302
|
clientBuilder.connectTimeout(options.timeout, TimeUnit.SECONDS);
|
|
303
|
+ clientBuilder.readTimeout(options.timeout, TimeUnit.SECONDS);
|
297
|
304
|
}
|
298
|
305
|
|
299
|
306
|
OkHttpClient client = clientBuilder.build();
|
300
|
307
|
Call call = client.newCall(req);
|
301
|
308
|
taskTable.put(taskId, call);
|
302
|
309
|
call.enqueue(new okhttp3.Callback() {
|
|
310
|
+
|
303
|
311
|
@Override
|
304
|
312
|
public void onFailure(Call call, IOException e) {
|
305
|
|
- callback.invoke(e.getLocalizedMessage(), null);
|
|
313
|
+ if(respInfo == null) {
|
|
314
|
+ respInfo = Arguments.createMap();
|
|
315
|
+ }
|
|
316
|
+
|
|
317
|
+ // check if this error caused by timeout
|
|
318
|
+ if(e.getClass().equals(SocketTimeoutException.class)) {
|
|
319
|
+ respInfo.putBoolean("timeout", true);
|
|
320
|
+ callback.invoke("request timed out.", respInfo, null);
|
|
321
|
+ }
|
|
322
|
+ else
|
|
323
|
+ callback.invoke(e.getLocalizedMessage(), respInfo, null);
|
306
|
324
|
}
|
307
|
325
|
|
308
|
326
|
@Override
|
309
|
327
|
public void onResponse(Call call, Response response) throws IOException {
|
310
|
328
|
ReadableMap notifyConfig = options.addAndroidDownloads;
|
|
329
|
+ respInfo = getResponseInfo(response);
|
311
|
330
|
// Download manager settings
|
312
|
331
|
if(notifyConfig != null ) {
|
313
|
332
|
String title = "", desc = "", mime = "text/plain";
|
|
@@ -333,7 +352,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
333
|
352
|
} catch (Exception error) {
|
334
|
353
|
error.printStackTrace();
|
335
|
354
|
taskTable.remove(taskId);
|
336
|
|
- callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause());
|
|
355
|
+ callback.invoke("RNFetchBlob request error: " + error.getMessage() + error.getCause(), this.respInfo);
|
337
|
356
|
}
|
338
|
357
|
}
|
339
|
358
|
|
|
@@ -405,10 +424,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
|
405
|
424
|
}
|
406
|
425
|
info.putMap("headers", headers);
|
407
|
426
|
Headers h = resp.headers();
|
408
|
|
- if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("text/plain")) {
|
|
427
|
+ if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("text/")) {
|
409
|
428
|
info.putString("respType", "text");
|
410
|
429
|
}
|
411
|
|
- else if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("application/json")) {
|
|
430
|
+ else if(getHeaderIgnoreCases(h, "content-type").contains("application/json")) {
|
412
|
431
|
info.putString("respType", "json");
|
413
|
432
|
}
|
414
|
433
|
else if(getHeaderIgnoreCases(h, "content-type").length() < 1) {
|