React-native fetch() from https server with self-signed certificate
Disclaimer: This solution should be temporary and documented so that it won't stay in the production phase of the software, this is for development only.
For iOS, all you have to do is, open your xcodeproject (inside your iOS folder in RN) once you have that open, go to RCTNetwork.xcodeproj and in that project, navigate to RCTHTTPRequestHandler.m
In that file you will see a line like this:
#pragma mark - NSURLSession delegate
right after that line, add this function
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
And voila, you can now make insecure calls to your API without a valid certificate.
That should be enough, but if you are still having problems, you might need to go to your project's info.plist, left click on it and choose open as... source code.
and at the end just add
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>subdomain.example.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
so your file will look like this
...
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>subdomain.example.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
</plist>
For a real production ready solution, https://stackoverflow.com/a/36368360/5943130 that solution is better
React-native : self-signed certification implementation
I use react-native-ssl-pinning, it's work fine with Certificate pinning.
And the real problem was coming from a bad install of server certificate.
How to disable ssl check in react native XMLHttpRequest API or in Fetch Api?
Now to bypass ssl certificate issue.. rn-fetch-blob released. Anyone searching for updated answer please use this answer and check the
rn-fetch-blob
package.
To connect a server with self-signed certification, you need to add trusty to config explicitly
RNFetchBlob.config({
trusty : true
})
.fetch('GET', 'https://example.com')
.then((resp) => {
// ...
})
Ignore self-signed SSL certificate in fetch
I fixed this by setting up the SSL certificates in the simulator: Help > SSL Proxying > Install Charles Root Certificate in iOS Simulators) in charles and be happy!
Thanks https://stackoverflow.com/a/35047215/82156
Related Topics
What Is the "Best" Way to Get and Set a Single Cookie Value Using JavaScript
Angular 2: Two Backend Service Calls on Success of First Service
How to Plug a JavaScript Engine with Ruby and Nokogiri
How to Auto Hide Alert Box After It Showing It
Improving Performance of Click Detection on a Staggered Column Isometric Grid
How to Get an Ajax Get-Request to Wait for the Page to Be Rendered Before Returning a Response
Is There a Jquery Autogrow Plugin for Text Fields
Difference of the Value, Prototype and Property
Assign Console.Log Value to a Variable
Why Is It Frowned Upon to Modify JavaScript Object's Prototypes
Why Doesn't Decodeuri("A+B") == "A B"
Losing "This" Context in JavaScript When Passing Around Members
The Invocation Context (This) of the Foreach Function Call
Convert Array of Strings into an Array of Objects