I am working to develop a Nativescript app that hits a backend API (running Laravel). For development, the app is being served locally using Laravel Valet on jwt9.test.
I am having issues with Axios accepting my self-signed certificate. The below code works brilliantly when hitting an outside API:
const axios = require('axios').default;
const instance = axios.create({
baseURL: 'https://jsonplaceholder.typicode.com/posts/',
});
instance.get('/1',
{
'email': email, // relic for local instance
'password': password // relic for local instance
}, {
"headers": {
'Content-Type': 'application/json',
}
})
.then(function (response) {
console.log("Dilly Dilly: " + JSON.stringify(response));
})
.catch(function (error) {
console.log("Zut: " + error)
});
}
When I flip it to the local server (change to a post with appropriate URL that is working just fine via Postman), I receive:
JS: 'Zut: Error: Request failed with status code null'
My research indicates that Axios doesn't like self-signed certificates.
I have seen some solutions that involve loading the LaravelValetCASelfSigned.pem into the emulator and/or having Axios load it, but details are sparse and documentation not full (I'm relatively new to all of this so if you are a grizzled vet and think it's simple, please elucidate!).
Per https://github.com/axios/axios/issues/535, if I try the suggested approach and install the node "https" module and load up:
var https = require('https');
while also in-lining in my request:
{
"httpsAgent": new https.Agent({ rejectUnauthorized: false })
},
I am told that TypeError: https.Agent is not a constructor
.
Dropping "new" results in https.Agent is not a function
.
For a non-developer, I feel I've made enormous headway, but the simple problem of how to create a functional local development environment using relatively common tools such as Nativescript, Laravel, Valet, and Axios is escaping me.
Any suggestions? I'm not keen to have a live development environment.