0

When I fetch the API in React native Expo project will get:

[TypeError: Network request failed].

I have try to fetch other API, it can get data success.

And also try to fix by this question but didn't work for me. (Also tried this React Native fetch() Network Request Failed)

Is API's problem?

My code like:

const val = { uId: 'molly', pwd: '123', id: '1' }
const url = 'https://c........com.tw:8899'

let header = {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify(val),
}
fetch(`${url}/auth/login`, header)
    .then((r) => r.json())
    .then((d) => {
        if (d.code === 0) {
            console.log(d.data)
        } else {
            console.log('MSG', d.msg)
        }
    })
    .catch((err) => console.log(err))

And I have try this, it can work. (So is not the network problem.)

fetch('https://randomuser.me/api/')
        .then((r) => r.json())
        .then((d) => {
            console.log('ok')
        })
        .catch((err) => console.log(err))
2
  • this solution can solve your answer please go through it stackoverflow.com/questions/38418998/… Commented Jan 6, 2023 at 5:55
  • Sorry, I've tried this solution too. It didn't work also. Commented Jan 6, 2023 at 8:23

1 Answer 1

0

I found the problem is about ssl. Should use this package to deal with

react-native-ssl-pinning

https://www.npmjs.com/package/react-native-ssl-pinning

Sign up to request clarification or add additional context in comments.

Comments

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.