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 eb84d6e33d9c24..0bfeb3f1ef4491 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 @@ -372,7 +372,7 @@ public void onProgress(long bytesWritten, long contentLength, boolean done) { return; } } else { - requestBody = RequestBody.create(contentMediaType, body); + requestBody = RequestBody.create(contentMediaType, body.getBytes(StandardCharsets.UTF_8)); } } else if (data.hasKey(REQUEST_BODY_KEY_BASE64)) { if (contentType == null) { diff --git a/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java b/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java index 39a5cf76533be7..cb6136e7f07404 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java +++ b/ReactAndroid/src/test/java/com/facebook/react/modules/network/NetworkingModuleTest.java @@ -247,7 +247,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable { body.putString("string", "This is request body"); mockEvents(); - + networkingModule.sendRequest( "POST", "http://somedomain/bar", @@ -309,6 +309,43 @@ public Object answer(InvocationOnMock invocation) throws Throwable { assertThat(requestHeaders.get("User-Agent")).isEqualTo("React test agent/1.0"); } + @Test + public void testPostJsonContentTypeHeader() throws Exception { + OkHttpClient httpClient = mock(OkHttpClient.class); + when(httpClient.newCall(any(Request.class))).thenAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Call callMock = mock(Call.class); + return callMock; + } + }); + OkHttpClient.Builder clientBuilder = mock(OkHttpClient.Builder.class); + when(clientBuilder.build()).thenReturn(httpClient); + when(httpClient.newBuilder()).thenReturn(clientBuilder); + NetworkingModule networkingModule = + new NetworkingModule(mock(ReactApplicationContext.class), "", httpClient); + + JavaOnlyMap body = new JavaOnlyMap(); + body.putString("string", "{ \"key\": \"value\" }"); + + networkingModule.sendRequest( + "POST", + "http://somedomain/bar", + 0, + JavaOnlyArray.of(JavaOnlyArray.of("Content-Type", "application/json")), + body, + /* responseType */ "text", + /* useIncrementalUpdates*/ true, + /* timeout */ 0, + /* withCredentials */ false); + + ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Request.class); + verify(httpClient).newCall(argumentCaptor.capture()); + + // Verify okhttp does not append "charset=utf-8" + assertThat(argumentCaptor.getValue().body().contentType().toString()).isEqualTo("application/json"); + } + @Test public void testMultipartPostRequestSimple() throws Exception { PowerMockito.mockStatic(RequestBodyUtil.class);