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
This commit is contained in:
Sandro Machado 2017-07-20 16:16:21 -07:00 committed by Facebook Github Bot
parent 8e8fecdcf8
commit bdd46aa283
1 changed files with 13 additions and 1 deletions

View File

@ -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);
}