# Agent.query in react native not fetching properly

**URL:** <https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990>\
**Category:** Developers\
**Created:** [July 19, 2021, 6:15pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990 "2021-07-19T18:15:46Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehsan6sha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ehsan6sha/32/2460_2.png) [@ehsan6sha](https://forum.dfinity.org/u/ehsan6sha)\
**Post date:** [July 19, 2021, 6:15pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/1 "2021-07-19T18:15:47Z")

</div>

Hi

It seems there is an issue in agent.query in react native and I was able to narrow down the issue. wanted to see if anyone has a solution for it?

In ‘query(canisterId, fields, identity)’ function inside agent library, it seems the fetch used does not support StreamReader in react native as it is used here and we need to think of a polyfill for it to be able to return correct value. any insight in much appreciated.

---

<div class="post-metadata">

**Author:** ![ehsan6sha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ehsan6sha/32/2460_2.png) [@ehsan6sha](https://forum.dfinity.org/u/ehsan6sha)\
**Post date:** [July 19, 2021, 6:17pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/2 "2021-07-19T18:17:20Z")

</div>

I can see the response when I use console.log(await response.text()), but response.arrayBuffer() is not returning anything. I believe it has something to do with StreamReader

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [July 21, 2021, 5:31am UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/3 "2021-07-21T05:31:31Z")

</div>

I think it’s related to an issue I found where RN doesn’t properly deal with binary response bodies. Basically I had to polyfill the default fetch. See [here](https://forum.dfinity.org/t/making-http-calls-from-mobile-apps/5192/14) for details.

---

<div class="post-metadata">

**Author:** ![ehsan6sha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ehsan6sha/32/2460_2.png) [@ehsan6sha](https://forum.dfinity.org/u/ehsan6sha)\
**Post date:** [July 21, 2021, 6:25pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/4 "2021-07-21T18:25:19Z")

</div>

Can you please share a part of code showing how you used and configured react-native-fetch-api ?

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [July 21, 2021, 8:38pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/5 "2021-07-21T20:38:53Z")

</div>

So the first step obviously is to apply the polyfill globally using:

```auto
import {polyfill as polyfillFetch} from 'react-native-polyfill-globals/src/fetch'
polyfillFetch();

```

I put this in a `shim.js` that is imported by the top-level `index.js`.

* * *

The second step is to patch `@dfinity/agent` to replace this [line](https://github.com/dfinity/agent-js/blob/579f8aa3fae946a9060bed39ff56c87151098c49/packages/agent/src/agent/http/index.ts#L204) with:

```auto
this._fetch('' + new URL(`/api/v2/canister/${ecid.toText()}/call`, this._host), Object.assign(Object.assign({reactNative: {textStreaming: true}}, transformedRequest.request), { body })),

```

Note that `{reactNative: {textStreaming: true}}` was added.

* * *

The last step is to patch `react-native-fetch-api` and replace the “blob” in this [line](https://github.com/react-native-community/fetch/blob/74f6a96ba1d720e6effd42ee9a47655b44ec9e3b/src/Fetch.js#L34) with “base64”.

* * *

Let me know if this works.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [July 21, 2021, 8:40pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/6 "2021-07-21T20:40:49Z")

</div>

BTW I use `patch-package` to patch these libraries.

---

<div class="post-metadata">

**Author:** ![ehsan6sha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ehsan6sha/32/2460_2.png) [@ehsan6sha](https://forum.dfinity.org/u/ehsan6sha)\
**Post date:** [July 21, 2021, 9:54pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/7 "2021-07-21T21:54:20Z")

</div>

> [@jzxchiang](#):
>
> base64

Thank you for the great guide. It seems some errors are gone, but I run into a new error, which I am debugging in case if you have any insights on this one

```auto
Input too short
at node_modules\borc\src\decoder.js:548:14 in Decoder#_decode
at node_modules\borc\src\decoder.js:576:2 in Decoder#decodeFirst
at node_modules\@dfinity\agent\lib\cjs\cbor.js:110:4 in decode
at node_modules\@dfinity\agent\lib\cjs\agent\http\index.js:172:4 in HttpAgent#query

```

---

<div class="post-metadata">

**Author:** ![ehsan6sha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ehsan6sha/32/2460_2.png) [@ehsan6sha](https://forum.dfinity.org/u/ehsan6sha)\
**Post date:** [July 21, 2021, 11:07pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/8 "2021-07-21T23:07:32Z")

</div>

So I can confirm my call is received by the canister(I see it in debug message)  
When my canister does not have a return everything is working, however, when my canister returns a Text (a simple one). and I query it using the agent library, it stops responding, and I cannot even call it using command line! any idea why?

After the very first call from agent, it logs the message and stops responding even to calls from command line. If there is no return, it works fine.

```auto
import Debug "mo:base/Debug";
actor HelloActor {
   public query func hello() : async Text () {
      Debug.print ("Hello, World from DFINITY \n");
	  return "Hello, World from DFINITY";
   }
};

```

Is there a log somewhere that hows what is blocking the requests?

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [July 21, 2021, 11:19pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/9 "2021-07-21T23:19:17Z")

</div>

You probably need to put a bunch of console.logs inside the agent-js library to see what part it’s hanging up on. It could be the BigInt polyfill being too slow.

Also, my (local) canister sometimes stops responding after it fills up with too much data, and after I restart it it works. Maybe try restarting your local dfx replica, delete and redeploy your canister, and make the request again? I’ve definitely gotten it working before with the Hello World example you put.

---

<div class="post-metadata">

**Author:** ![ehsan6sha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ehsan6sha/32/2460_2.png) [@ehsan6sha](https://forum.dfinity.org/u/ehsan6sha)\
**Post date:** [July 22, 2021, 4:44pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/10 "2021-07-22T16:44:31Z")

</div>

Again thank you for saving tons of time for me. It is working perfectly for me, however, for me it works if I do not put textStreaming: true, and does not work with it. But all looks good and much appreciated.

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [July 22, 2021, 9:46pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/11 "2021-07-22T21:46:42Z")

</div>

Yeah you only want to put that for the call() method, not for the other ones that return binary data. If that doesn’t work, then that’s surprising.

---

<div class="post-metadata">

**Author:** ![ehsan6sha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ehsan6sha/32/2460_2.png) [@ehsan6sha](https://forum.dfinity.org/u/ehsan6sha)\
**Post date:** [July 22, 2021, 10:10pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/12 "2021-07-22T22:10:33Z")

</div>

Ah, now I see! I put it for query! So that was the issue. thank you for mentioning it

---

<div class="post-metadata">

**Author:** ![ehsan6sha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ehsan6sha/32/2460_2.png) [@ehsan6sha](https://forum.dfinity.org/u/ehsan6sha)\
**Post date:** [July 30, 2021, 7:55pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/13 "2021-07-30T19:55:18Z")

</div>

Did you find a workaround for WebAssembly?

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [July 31, 2021, 4:37am UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/14 "2021-07-31T04:37:16Z")

</div>

It’s a pretty involved process. Basically, you can’t use WebAssembly and need to do the BLS signature validation in JS instead.

One way is to transpile the wasm bytecode in `@dfinity/agent` to JS using `wasm2js` and run that instead. It’s pretty slow though. Another way is to use the JS code in this repo: [GitHub - miracl/core: MIRACL Core](https://github.com/miracl/core). It was ~3x faster than the `wasm2js` solution but still 3x faster than if you just comment out the BLS signature validation logic.

@anon74414410 mentioned that implementing it native-side could be another option, but it will definitely take more work.

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [August 1, 2021, 6:00pm UTC](https://forum.dfinity.org/t/agent-query-in-react-native-not-fetching-properly/5990/15 "2021-08-01T18:00:06Z")

</div>

@chenyan We should try to do a proof of concept by building the BLS validation to C and then importing it into a React Native app
