In my react native app I am trying to perform a fetch
to my local backend server. In my package.json
I have put "proxy": "http://localhost:3000"
.
My fetch looks like:
fetch('/')
.then(response => response.json())
.then(json => console.log(json))
.catch(err => console.log)
It catches an error
[TypeError: Network request failed]
When I remove the proxy and manually enter the address in the fetch it works and my server receives a GET request.
fetch('http://localhost:3000')
.then(response => response.json())
.then(json => console.log(json))
.catch(err => console.log)
In my react native app I am trying to perform a fetch
to my local backend server. In my package.json
I have put "proxy": "http://localhost:3000"
.
My fetch looks like:
fetch('/')
.then(response => response.json())
.then(json => console.log(json))
.catch(err => console.log)
It catches an error
[TypeError: Network request failed]
When I remove the proxy and manually enter the address in the fetch it works and my server receives a GET request.
fetch('http://localhost:3000')
.then(response => response.json())
.then(json => console.log(json))
.catch(err => console.log)
Share
Improve this question
edited Jul 28, 2023 at 16:11
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Feb 23, 2019 at 9:01
siddrajpootsiddrajpoot
1831 silver badge7 bronze badges
2 Answers
Reset to default 7Two points here:
Set
proxy
inpackage.json
is a feature of create-react-app, which wraps webpack dev server's proxy. react-native uses Metro instead of Webpack as bundler and it does not support setting up a local proxy.Different from the web, http client in react-native has no current
host
. So if you usefetch('/')
, it won't know which domain or ip address to send request to.
hello you can use your network IP Address for using local server like instead localhost change with your Ip address and you can call your server e.g, http://192.xxx.x.xxx:3000