8

Environment

I'm developing a mobile app with React Native and Expo.

Also, developing api with Laravel that is in my local environment. (http://localhost:8000/)

This app is working on Expo client app.

What I'm trying to do

I wanna get datas from api.

Code

App.js

componentDidMount() {
  return fetch('http://localhost:8000/api/test')
    .then((response) => response.json())
    .then((responseJson) => {
      console.log(responseJson);
    })
    .catch((error) => {
      console.error(error);
    });
}

Error

TypeError: Network request failed

Expo version

3.22.3

I would appreciate it if you could give me any advice.

3
  • 1
    Maybe https instead? Commented Jul 17, 2020 at 3:07
  • @高鵬翔 Thanks. I think it should be http. Commented Jul 17, 2020 at 4:08
  • 2
    iOS won't make api calls to http, there are some additional steps required to make that happen, google it or switch to https Commented Jul 17, 2020 at 5:13

3 Answers 3

8

you should not use http://localhost:8000 for your base URL, please use your IP Address.

if you use Macbook, you can find the IP in System Preferences > Network

find ip

then use fetch('http://172.20.10.5:8000/api/test') in your react native app.

if my base URL when access via browser is http://172.20.10.5:8000/mysite/api/test you need fetch in react native fetch('http://172.20.10.5:8000/mysite/api/test')

basically, android & ios did not allow request using HTTP, you need to do something below to allow HTTP connection instead of HTTPS.

android

and then add android:usesCleartextTraffic="true" in your Android manifest android/app/src/main/AndroidManifest.xml

iphone

add this in info.plist

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>localhost</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you. I changed to my IP address, but it didn't work :(
can i know your full URL in your Browser/Postman for accessing the datas? also make sure your computer (server) and your device must be on the same local network. Wifi and lan shares the same network.
My full URL is localhost:8000/api/test. My computer and iPhone are on the same network.
add android:usesCleartextTraffic="true" in Android manifest
Is that related? I'm using expo client app on iPhone.
|
5

Try using http://10.0.2.2/api/test

Emulators has special IP Address for localhost connection.

Check this table out

Read about it here

Comments

-1

if after this it not work, disable you firewall

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.