-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fetch adds charset=utf-8 on Android but not on iOS #8237
Comments
I'm also experiencing this. Having different headers between platforms doesn't make sense and introduces bugs |
What's the right thing to do here - should the charset be appended or not? |
Having the same issue while working with Urban Airship APIs. Frustrating as UA supposed to be not changing at their end. What to do? |
Im also seeing this |
Looking at Chrome, it doesn't modify the content-type header at all so we shouldn't be adding a charset. |
I'm also experiencing this issue, somehow In my specific case, I'm interacting with a JSON API (http://jsonapi.org/format/) and adding media type parameters causes the server to respond with a 415 error every time. This pretty much breaks the app :( Edit: I've debugged deep down into
and, even here the |
I'm also having this problem and, while doing some testing, I've tried setting an invalid charset "dropbox-cors-hack" (as I'm testing the Dropbox API) so the request was like this:
When doing so I got the following exception: And that lead me to this file from the okhttp library: https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/RequestBody.java#L53 So it seems it appends "charset=utf-8" if the charset is not set. I'm not sure if this is React-Native that is calling this lib or if it's Android, but is there anything that can be done to prevent this behaviour? |
Based on this comment it doesn't look like it will be fixed on the okhttp side: They suggest using the |
@laurent22 you could customize okhttp client on android that is used under the hood to implement fetch. You can add interceptor to this client and modify any header before it is being processed. |
@Knight704 our discussion in other thread was about keeping a single OkHttp instance. I don't understand how that helps to fix this behavior in OkHttp. Are you overriding entire NetworkingModule to call |
@hey99xx the idea boils down to put your custom interceptor to okhttpclient. Yes, okhttp will continue appending charset into header, but inside interceptor you can modify any headers before it really goes to the network, i.e cutting down charset part. This is of course a workaround, but it'll work. Another point that okhttp isn't easy to access right now, so I provided link to solution to that problem as well |
@Knight704 How would you know if the "Content-type" is automatically set to have the charset part by OkHttp, and not by manually adding the header during On my end, I've managed to fix the issue by contacting the backend people to be tolerant to the charset header, but I don't think that's doable when you contact external APIs such as AWS and Dropbox. |
@hey99xx yeah, I've already mentioned that this is just a hack, and preferably it should be fixed in SDK. |
For anyone wanting a copy/paste example, follow the steps outlined here (thanks heaps for that by the way @Knight704), then you can copy my version of
|
@jeremy8883, in which directory i copy and past your code ? in src/java/com/ ? your class resolve the problem for appending "charset=utf-8" to content-type ? |
Yeah, plus the package location, so
That's correct |
I use I agree with @laurent22 , can we change Fetch implementation to use okhttp's Note: @Knight704 's hack won't work with CRNA/Expo due to unable to access Android code |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions. |
This is still an issue in lates RN , please don’t close
…On Sun, 17 Jun 2018 at 5:37 stale[bot] ***@***.***> wrote:
Hey there, it looks like there has been no activity on this issue
recently. Has the issue been fixed, or does it still require the
community's attention? This issue may be closed if no further activity
occurs. You may also label this issue as "For Discussion" or "Good first
issue" and I will leave it open. Thank you for your contributions.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#8237 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AB4Q-EzEsJzM5KqNZBsVTbJAIO6sqFAeks5t9cDkgaJpZM4I5aJ7>
.
|
+1 |
This is still an issue |
To resolve this problem, use lib axios |
Axios is a javascript library. The problem is in the RN/okhttp java implementation. |
Axios work for me in IOS |
@programadornatal iOS doesn't have this bug even using pure fetch, without using axios. This is Android specific. |
Axios has the same problem on Android. There is a topic describing the same problem but the solution of changing the default header does not seem to work. Android (or RN) seems to be modifying it on a lower level. |
This seemed like a fun one to dig into, so I proposed a fix here: |
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: square/okhttp#2099 (comment) Related issues: #8237 [Android][fixed] - Prevent fetch() POST requests on Android from appending `charset=utf-8` to `Content-Type` header. Pull Request resolved: #23580 Differential Revision: D14180849 Pulled By: cpojer fbshipit-source-id: b84cadf83361331a9f64d1ff5f2e6399a55527a6
@nhunzaker I know I approved your PR but just looking again, I wanted to clarify this. I cannot ask on the PR itself since it's locked already. You had mentioned
Do you still think it'd be better? One use case that came to my mind is setting a custom content-type header like: In that case OkHttp's I really don't know what the correct implementation is supposed to be. Maybe only default to utf-8 is MediaType is null or charset-less? That's what OkHttp internally does. |
Example of how this failure could be> I've never written a raw http server though so cant tell if this is how it works:
output is:
|
@hey99xx you are correct. my implementation needs to respect the existing character set. I have a test case that reproduces the issue, I'll send out another PR. The code I changed needs to really be something like:
|
This commit fixes a bug introduced in a previous attempt to address an issue where okhttp appended `charset=utf-8` to the Content-Type header when otherwise not specified. In that commit, I converted all characters to UTF-8, however it should instead use an existing encoding when possible. Related issues: facebook#8237 (comment)
Summary: This commit fixes a bug introduced in a previous attempt (#23580) to address an issue where okhttp appended `charset=utf-8` to the Content-Type header when otherwise not specified. In that commit, I converted all characters to UTF-8, however it should instead use an existing encoding when possible. Related issues: #8237 (comment) [Android][fixed] - Respect existing character set when specified in fetch() POST request Pull Request resolved: #23603 Differential Revision: D14191750 Pulled By: hramos fbshipit-source-id: 11c1bfd98ccd33cd8e54ea426285b7d2ce9c2d7c
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: square/okhttp#2099 (comment) Related issues: #8237 [Android][fixed] - Prevent fetch() POST requests on Android from appending `charset=utf-8` to `Content-Type` header. Pull Request resolved: #23580 Differential Revision: D14180849 Pulled By: cpojer fbshipit-source-id: b84cadf83361331a9f64d1ff5f2e6399a55527a6
Summary: This commit fixes a bug introduced in a previous attempt (#23580) to address an issue where okhttp appended `charset=utf-8` to the Content-Type header when otherwise not specified. In that commit, I converted all characters to UTF-8, however it should instead use an existing encoding when possible. Related issues: #8237 (comment) [Android][fixed] - Respect existing character set when specified in fetch() POST request Pull Request resolved: #23603 Differential Revision: D14191750 Pulled By: hramos fbshipit-source-id: 11c1bfd98ccd33cd8e54ea426285b7d2ce9c2d7c
The fixes are in 0.59.0-rc.3 which is just released. Can someone verify the problem is gone and we can then resolve this issue? |
Confirmed that this works on RN 0.59.1! |
This fetch request sends "application/json" as ContentType on IOS but "application/json; charset=utf-8" on Android:
The server I'm working against requires application/json and fails when charset is added. I've filed a bug report with them too, but shouldn't fetch behave identically on ios and android?
The text was updated successfully, but these errors were encountered: