Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...