How can i reach other computer api react native

Viewed 23

im developing my react native app on MAC and im developing my API from my windows pc.

when im try to reach api from mac with postman i can use

but when i try to send request to api from react-native app (ios or android emulator) it cant reach.

postman request is: Method GET http://192.168.1.109:5000/api/pk/get it returns data on MAC successfully

when i trying to code below:

fetch("http://192.168.1.109:5000/api/pk/get").then(response=>{console.log(response.json())})

it gives NETWORK REQUEST FAILED error.

how can i make this connection on same network two devices (Actually it work on postman. how can i make reach react-native app to other pc API)

1 Answers

The .json() method is asynchronous. You need to add another callback to handle the data from the resolved promise. Change it to this:

fetch("http://192.168.1.109:5000/api/pk/get")
   .then(response => {
    response.json()
        .then(data => console.log(data))
    }

You'll likely also need to enable HTTP for iOS. Add this to your info.plist file

<key>NSAppTransportSecurity</key> 
    <dict>     
        <key>NSAllowsArbitraryLoads</key>     
        <true/> 
    </dict>

See this answer for info on how to do it through XCode.

Related