0

I'm so tired to fix this problem i'ts work from https and it's do not work in http please help me

when i am using this api => https://jsonplaceholder.typicode.com/posts i'ts work but when i use my api it doesn't work then

here is my code

import axios from 'axios'

let baseURL = ''

{
  Platform.OS == 'android'
  ? baseURL = 'http://10.0.2.2:3000/api/v1/' // android
  : baseURL = 'http://localhost:3000/api/v1/' // IOS
}

axios.get(`${baseURL}products`)
.then(res => {console.log(res) })
.catch(err => console.log(err)})

i'ts has error

Network Error at http://packager.x5-xy3.anonymous.g-bazar.exp.direct/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:189790:25 in createError at node_modules\axios\lib\adapters\xhr.js:91:24 in handleTimeout at node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:600:10 in setReadyState at node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in didCompleteResponse at node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in callFunction at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in guard$argument_0 at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in guard at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue at [native code]:null in callFunctionReturnFlushedQueue

6
  • It seems like your backend block the request, you need to set the header of the response in the backend, please check this link
    – Morta
    Jun 8, 2021 at 7:54
  • you need to allow http requests for iOS. Otherwise you can't access http requests
    – Manojkanth
    Jun 8, 2021 at 8:19
  • @MortadhaFadhlaoui my link is okay but it has error if you know another way please help me
    – MEAbid
    Jun 8, 2021 at 11:55
  • @Coder where I allow the http request for IOS, why I can't use http ?
    – MEAbid
    Jun 8, 2021 at 11:56
  • @Coder is right, check this link
    – Morta
    Jun 8, 2021 at 12:13

1 Answer 1

0

iOS sdk enforces connection to be in https protocol instead of http.you can add an exception to your domain inside info.plist file of the Xcode project.

Open your info.plist file of your project with any editor of your preference, then add this code at the end of the file before the last

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

check this link

1
  • do you have another way to fix it ?
    – MEAbid
    Jun 8, 2021 at 15:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.