From bdd46aa28392004c7d29e59434d6c8d95c6f36c8 Mon Sep 17 00:00:00 2001 From: Sandro Machado Date: Thu, 20 Jul 2017 16:16:21 -0700 Subject: [PATCH] Fix `HEAD` http requests in Android Summary: Closes https://github.com/facebook/react-native/issues/7463. This PR fixes the `HEAD` http requests in Android. In Android all the HEAD http requests will fail, even if the request succeeds in the native layer due to an issue in the communication with the `OkHttp`. Closes https://github.com/facebook/react-native/pull/14289 Differential Revision: D5166130 Pulled By: hramos fbshipit-source-id: a7a0deee0fcb5f6a645c07d4e6f4386b5f550e31 --- .../react/modules/network/NetworkingModule.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java index d4b047994..297cd1ef4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java @@ -369,7 +369,19 @@ public final class NetworkingModule extends ReactContextBaseJavaModule { // Otherwise send the data in one big chunk, in the format that JS requested. String responseString = ""; if (responseType.equals("text")) { - responseString = responseBody.string(); + try { + responseString = responseBody.string(); + } catch (IOException e) { + if (response.request().method().equalsIgnoreCase("HEAD")) { + // The request is an `HEAD` and the body is empty, + // the OkHttp will produce an exception. + // Ignore the exception to not invalidate the request in the + // Javascript layer. + // Introduced to fix issue #7463. + } else { + ResponseUtil.onRequestError(eventEmitter, requestId, e.getMessage(), e); + } + } } else if (responseType.equals("base64")) { responseString = Base64.encodeToString(responseBody.bytes(), Base64.NO_WRAP); }