16

I'm trying to make https requests to the server using axios. Most of the tutorials regarding axios specify how to make http requests. I make the requests whenever users login. Here is my current request:

axios.post('/api/login/authentication', {
  email: email,
  password: password
})
.then(response => {
  this.props.history.push('/MainPage')
})
.catch(error => {
  console.log(error)
})

Can anyone help me convert this to an https request?

1
  • 1
    If your server has a https certificate, it should automatically default to that Commented Dec 2, 2018 at 3:26

2 Answers 2

11

All URLs have two parts

  1. Domain - http://yourdomain.com
  2. Path - /path-to-your-endpoint

1. Use default domain

In axios, if you specify just the path, it will use the domain in the address bar by default.

For example, the code below will make a call to whatever domain is in your address bar and append this path to it. If the domain is http, your api request will be a http call and if the domain is https, the api request will be a https call. Usually localhost is http and you will be making http calls in localhost.

axios.post('/api/login/authentication', {

2. Specify full URL with domain

On the other hand, you can pass full URL to axios request and you will be making https calls by default.

axios.post('https://yourdomain.com/api/login/authentication', {

2. Use axios baseURL option

You can also set baseURL in axios

axios({
  method: 'post',
  baseURL: 'https://yourdomain.com/api/',
  url: '/login/authentication',
  data: {
    email: email,
    password: password
  }
}).then(response => {
  this.props.history.push('/MainPage')
})
.catch(error => {
  console.log(error)
});
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer. I'm getting the errors: Failed to load resource: An SSL error has occurred and a secure connection to the server cannot be made. and XMLHttpRequest cannot load https://localhost:3000/api/login/authentication due to access control checks. could this be another issue or one related to how I'm sending the request
Can you open that HTTPS Url in your browser? Do you get any SSL certificate errors? Are you sure the port number is correct (it cannot be the same as for HTTP)?
The error you are facing is not related to the request. 1. localhost is usually only http. 2. To enable https you need to setup certificates in your server.
0

This may also work

 headers: {
        scheme: 'https',
      },

2 Comments

Please elaborate where to put it.
In the axios option argument

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.