Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reactjs make https (not http) requests with axios

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?

like image 633
jim Avatar asked Dec 02 '18 02:12

jim


1 Answers

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)
});
like image 63
Dinesh Pandiyan Avatar answered Nov 03 '22 22:11

Dinesh Pandiyan