axios / axios

Promise based HTTP client for the browser and node.js
https://axios-http.com
MIT License
105.77k stars 10.97k forks source link

Q: How can I make a HTTPS to the backend with a self-signed certificate? #1185

Closed uynap closed 6 years ago

uynap commented 6 years ago

I use the HTTPS backend with a self-signed certificate. I need to tell the HTTP client(fetch) to skip checking the insecure certificate. Otherwise I'm getting "x509: certificate signed by unknown authority" error. As I didn't find any related topics in the documentation. I was wondering what would be your solution or workaround for my situation?

uynap commented 6 years ago

BTW. I've tried rejectUnauthorized: false. It's just not working. https://github.com/axios/axios/issues/535

ghost commented 6 years ago

I'm stuck on this issue too. #535 suggests to create a custom https agent and set rejectUnauthorized to false however, https module doesn't exist in node_modules. Is there any workaround for this in react native?

florianbepunkt commented 6 years ago

+1 Same here

jcrben commented 6 years ago

In addition to passing the proper parameter in the constructor (new https.Agent({ rejectUnauthorized: false })), I needed to put: https.globalAgent.options.rejectUnauthorized = false; at the top (per https://github.com/request/request/issues/418#issuecomment-274105600)

You might also try process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

Similar recommendations appear in https://github.com/axios/axios/issues/535

@nickuraltsev @rubennorte seems like this question should be closed - if someone tries this combination and it still doesn't work, should reopen

xemasiv commented 6 years ago

Also encountered this on axios with react-native

Just use a Free SSL that isn't self-signed instead.

Free SSL & React Native Apps

Problem:

Solution:

  1. Generate a CSR
  1. Go get a Free SSL catered by Comodo.
  1. Do a CA/End-Entity Certificate matching
  1. Configure your Express SSL configuration
const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');
const bodyParser = require('body-parser');

const SSL = {
  credentials: {
    key: fs.readFileSync('ssl/private.key', 'utf8'),
    cert: fs.readFileSync('ssl/api_realtycoast_io.crt', 'utf8'),
    ca: [
      fs.readFileSync('ssl/COMODORSADomainValidationSecureServerCA.crt', 'utf8')
    ]
  }
};

const app = express();
app.use(bodyParser.json());
app.get('/', (req, res) => res.send('Hello World!'));
http.createServer(app).listen(80);
https.createServer(SSL.credentials, app).listen(443);
  1. Verify that shit
  1. Rebuild your react-native app, and re-run it - should be workign well now
import { WebView, View } from 'react-native';

// in your component render(), below

return (
<View style={{flex:1}}>
    <WebView
    source={{uri: 'https://api.realtycoast.io/auth'}}
    javaScriptEnabled={true}
    domStorageEnabled={true}
    startInLoadingState={true}
    style={{ flex:1, height: 500, width: 350 }}
    />
    </View>
);
    // replace your baseURL and url, below

          <Button onPress={
            ()=>{
              const x = axios.create({
                baseURL: 'https://api.realtycoast.io/',
                timeout: 10000,
                headers: {
                  'Accept': 'application/json',
                  'Content-Type': 'application/json',
                }
              });
            x.request({
              url: '/user/123'
            })
              .then(function (response) {
                console.log(response);
              })
              .catch(function (error) {
                console.log(error);
              });
            }
          }>
            <Text>Axios Test</Text>
          </Button>

https://github.com/xemasiv/my-dev-fixes#free-ssl--react-native-apps

shamilsun commented 5 years ago

@xemasiv this is not a solution, its example how to add ssl on nodejs backend. but we are looking how to allow self-ssl requests on android . okhttp3 restrict self signed certificate

shamilsun commented 5 years ago

with axios

selmi-karim commented 4 years ago

is there any update on this issue ??