Prevent okhttp from adding ;charset=utf8 to ContentType Header (#23580)

Summary:
Before this commit, `fetch()` calls append `"charset=utf8"` to the `Content-Type` header on Android (and not on iOS). This is because of an implementation detail in the okhttp library. This means that you can make a call on the JavaScript side like:

```javascript
let body = JSON.stringify({ key: "value" });

let headers = {
  "Content-Type": "application/json"
};

fetch("http://10.0.2.2:3000", { method: "POST", body, headers });
```

However the resulting request appends the utf8 character:

```
POST - 13:34:32:
  content-type: application/json; charset=utf-8
  content-length: 15
  host: 10.0.2.2:3000
  connection: Keep-Alive
  accept-encoding: gzip
  user-agent: okhttp/3.12.1
```

Passing byte array into the RequestBody avoids this, as recommended by a maintainer of okhttp:

https://github.com/square/okhttp/issues/2099#issuecomment-366757161

Related issues:
https://github.com/facebook/react-native/issues/8237

[Android][fixed] - Prevent fetch() POST requests on Android from appending `charset=utf-8` to `Content-Type` header.
Pull Request resolved: https://github.com/facebook/react-native/pull/23580

Differential Revision: D14180849

Pulled By: cpojer

fbshipit-source-id: b84cadf83361331a9f64d1ff5f2e6399a55527a6
This commit is contained in:
Nate Hunzaker
2019-02-21 22:33:18 -08:00
committed by Facebook Github Bot
parent 665954efcc
commit 4a807761a4
2 changed files with 39 additions and 2 deletions
@@ -372,7 +372,7 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
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) {