i have the problem, that i can not do a fetch request to localhost api. If a have my business logic on a server, everything works.
I use iOS and react-native 0.68.2.
If i run my app with xCode a get the following error:
2022-06-24 19:24:35.088109 0200 MyApp[46047:7055248] Task <409DD042-5979-
4A48-8781-C1E90F7AC8A4>.<4> finished with error [-1004] Error Domain=NSURLErrorDomain
Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61,
NSUnderlyingError=0x280e62160 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "
(null)" UserInfo={_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: lo0,
_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}},
_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <409DD042-5979-4A48-8781-
C1E90F7AC8A4>.<4>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <409DD042-5979-4A48-8781-C1E90F7AC8A4>.<4>"
), NSLocalizedDescription=Could not connect to the server.,
NSErrorFailingURLStringKey=http://localhost/MyApp/business/public/api/get-offerings,
NSErrorFailingURLKey=http://localhost/MyApp/business/public/api/get-offerings,
_kCFStreamErrorDomainKey=1}
flipper: Desktop failed to provide certificates. Error from flipper desktop:
rsocket::StreamInterruptedException: connection error
I already add the following in my Info.plist, but i get after this an error
<key>NSAllowsLocalNetworking</key>
<true>
Error:
The operation couldn’t be completed
Can someone help me why i cant do a request to localhost? I use XAMPP and the server is running. If i use PostMan to Test my request, everything works with the given Error (URL is included in the error). Should i use a IP? If i fetch to a server, everything works with Postman and request in the app. If i use instance of "localhost", "127.0.0.1" i also get the error "network request failed"
CodePudding user response:
The solution of this problem was not to use "localhost". I used my IP from the network and it works.
I got my solution from this post React Native fetch() Network Request Failed
CodePudding user response:
Open your Xcode project and under info tab add these settings..
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
It should work.