From c60b581327e5f3c8aebb1ebd5ecb5550ba2ffa76 Mon Sep 17 00:00:00 2001 From: Qiao Liang Date: Sat, 12 Dec 2015 09:09:09 -0800 Subject: [PATCH] fix POST/PUT/PATCH has no body redbox, when xhr is used without body MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: … by passing a empty body fix #3371 referring to https://github.com/square/okhttp/pull/1559/files Closes https://github.com/facebook/react-native/pull/4518 Reviewed By: svcscm Differential Revision: D2753086 Pulled By: lexs fb-gh-sync-id: 5c486b127b194b29cd0f8a2cb9a1ef19449109e3 --- .../react/modules/network/NetworkingModule.java | 2 +- .../react/modules/network/RequestBodyUtil.java | 12 ++++++++++++ 2 files 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 1156aa2fd..728acf7fe 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 @@ -136,7 +136,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule { requestBuilder.headers(requestHeaders); if (data == null) { - requestBuilder.method(method, null); + requestBuilder.method(method, RequestBodyUtil.getEmptyBody(method)); } else if (data.hasKey(REQUEST_BODY_KEY_STRING)) { if (contentType == null) { onRequestError(requestId, "Payload is set but no content-type header specified"); diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java index 7ce69c37e..f834120ac 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/RequestBodyUtil.java @@ -27,6 +27,7 @@ import com.squareup.okhttp.MediaType; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.internal.Util; import okio.BufferedSink; +import okio.ByteString; import okio.Okio; import okio.Source; @@ -112,4 +113,15 @@ import okio.Source; } }; } + + /** + * Creates a empty RequestBody if required by the http method spec, otherwise use null + */ + public static RequestBody getEmptyBody(String method) { + if (method.equals("POST") || method.equals("PUT") || method.equals("PATCH")) { + return RequestBody.create(null, ByteString.EMPTY); + } else { + return null; + } + } }