question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android RNFetchBlob.fetch doesn't decode response body as utf8, sometimes

See original GitHub issue

We are using rn-fetch-blob to do all networking in our app, and are running into an issue on Android where sometimes JSON returned from our server is not properly decoded by rn-fetch-blob. Basically, Chinese, Japanese, and other non-ASCII characters get all messed up in the response object that is sent to our javascript code. It is still “valid” JSON, and all ascii characters are fine, but the non-ASCII characters are messed up.

Basically a response from the server that should look like this:

{ "name": "Chinese - 让教师及学生更为有效地操作" }

Is getting returned to us from rn-fetch-blob as this:

{ "name": "Chinese - 让教师及学生更为有效地操作" }

Sometimes! This is important to note. Lots of non-ASCII text does seem to be fine and rn-fetch-blob behaves as expected. I am not 100% sure what characters cause the problem to manifest, but I am sure I know where the problem is after debugging rn-fetch-blob and have a good idea of what the fix might be.

The problem is this line: https://github.com/joltup/rn-fetch-blob/blob/master/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java#L512

That line is taking the bytes of the response body and encoding them into utf8. Sometimes it throws a CharacterCodingException and then the catch body sends the bytes through some kind of base 64 encoding which I dont understand but basically sends the JSON back to the app without decoding it from utf8. Thus, the problem.

We can remove that line, and everything is ok. That is one possible fix.

However, I think what the code wants to be doing is decoding the bytes FROM utf8 (not encoding them INTO utf8, because the response body is already in utf8).

I could be totally off base here. But if I replace that line with: Charset.forName("UTF-8").newDecoder().decode(ByteBuffer.wrap(b));

Everything works ok and I feel like that probably honors the intention of the code.

(I also think this is what react-native is doing, though also to be honest I dont understand the flow of their networking code well: https://github.com/facebook/react-native/blob/1151c096dab17e5d9a6ac05b61aacecd4305f3db/ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressiveStringDecoder.java#L67)

Anyways, I am happy to submit a PR if one of those things sounds like an appropriate fix, just let me know.

  • RN 0.57.1
  • rn-fetch-blob 0.10.13

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:5

github_iconTop GitHub Comments

2reactions
YoshiteruIwasakicommented, Feb 5, 2019

The same thing happened to me. Like this text.

(๑•̀ㅂ•́)و✧

(๑˃̵ᴗ˂̵)و

I set “RNFB-Response” to utf8. https://github.com/joltup/rn-fetch-blob/wiki/Fetch-API#rnfb-response-base64--utf8

'RNFB-Response': 'utf8',

But this setting cause return blank text.

https://github.com/joltup/rn-fetch-blob/blob/master/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java#L522

callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, "");

So, I fixed like this.

callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, new String(b));
1reaction
mo-ah-dawoodcommented, Mar 11, 2019

happened to me with arabic

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-native-fetch-blob - npm
A module provides upload, download, and files access API. Supports file stream read/write for process large files.. Latest version: 0.10.8, ...
Read more >
fetch / response.text() messes up the utf-8 - Stack Overflow
I havent had any problem with fetching other sources so far but now I keep getting a issue with the decoding/encoding.
Read more >
the deferred dom node could not be resolved - You.com
ajax() doesn't freeze the code while it checks the '/password' page on your server - it sends a request to the server and...
Read more >
Profile for Snyk Ltd - Linknovate
In order to make the open source community secure, the Snyk Security Team often conducts research into vulnerabilities, such as HTTP request smuggling,...
Read more >
W3layouts cms exploit - Weebly
When running flutter doctor in cmd show error some android studio license is ... decode byte 0x85 in position 715: invalid start byte...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found