Issue
I've been trying to run requests using AXIOS as part of my react native app. Here is my code snippet:
const response = await axios({
method: 'get',
url: this.url('https://my-domain:5000/api/v1/version'),
signal: this.newAbortSignal(5000),
//headers: this.headers
})
My abort signal function:
newAbortSignal(timeoutMs: number) {
const abortController = new AbortController();
setTimeout(() => abortController.abort(), timeoutMs || 0);
return abortController.signal;
}
The domain is trusted CA and verified so the https requests should work. I also tried going to that endpoint manually from the device and it works perfectly fine from the browser. I've defined no headers on purpose in order to exclude a cors issue. I am receiving a ERR_NETWORK status code from AXIOS. On the Server side I am not even receiving any incoming requests, like it never leaves the phone.
I tried this on both debug mode on a physical device and also a signed APK release version on the same device and both do not work.
Another attempt I tried just running a get request to 'https://google.com' and it failed with ERR_CANCELLED which I am not sure what that means.
FYI I also tried node fetch and it didn't work as well. I'd really appreciate some help :/
Solution
I had 2 problems that prevented from my code to work.
First issue was that I incorrectly signed my Express server. When signing your express server make sure you use your key, crt, and ca-bundle files.
const privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
const certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
const ca = fs.readFileSync('sslcert/ca.ca-bundle', 'utf8');
const credentials: https.ServerOptions = {key: privateKey, cert: certificate, ca: ca};
https
.createServer(credentials, app)
...
...
My second issue was the way I used Axios that prevented this from working. For some reason creating an axios client and then using it as following:
const client = new Axios(..)
await client.get(....)
It returned some weird behavior throwing some unexplained android exception about a Double not being able to be null.
When I moved to using:
const response = await axios({
method: 'get',
url: '...',
...
})
It worked fine.
Answered By - Dani Tseitlin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.