Axios를 사용하여 다음과 같은 HTTP 게시물을 수행합니다.
import axios from 'axios'
params = {'HTTP_CONTENT_LANGUAGE': self.language}
headers = {'header1': value}
axios.post(url, params, headers)
이 올바른지? 아니면 내가해야합니까 :
axios.post(url, params: params, headers: headers)
Axios를 사용하여 다음과 같은 HTTP 게시물을 수행합니다.
import axios from 'axios'
params = {'HTTP_CONTENT_LANGUAGE': self.language}
headers = {'header1': value}
axios.post(url, params, headers)
이 올바른지? 아니면 내가해야합니까 :
axios.post(url, params: params, headers: headers)
답변:
이를 수행하는 몇 가지 방법이 있습니다.
단일 요청의 경우 :
let config = {
headers: {
header1: value,
}
}
let data = {
'HTTP_CONTENT_LANGUAGE': self.language
}
axios.post(URL, data, config).then(...)기본 전역 구성 설정 :
axios.defaults.headers.post['header1'] = 'value' // for POST requests
axios.defaults.headers.common['header1'] = 'value' // for all requestsAxios 인스턴스에서 기본값으로 설정하는 경우 :
let instance = axios.create({
headers: {
post: { // can be common or any other method
header1: 'value1'
}
}
})
//- or after instance has been created
instance.defaults.headers.post['header1'] = 'value'
//- or before a request is made
// using Interceptors
instance.interceptors.request.use(config => {
config.headers.post['header1'] = 'value';
return config;
});axios여기 에서 관련 질문을 보 시겠습니까? : stackoverflow.com/questions/59470085/…
헤더를 사용하여 get 요청을 보낼 수 있습니다 (예 : jwt를 사용한 인증).
axios.get('https://example.com/getSomething', {
headers: {
Authorization: 'Bearer ' + token //the token is a variable which holds the token
}
})
또한 게시물 요청을 보낼 수 있습니다.
axios.post('https://example.com/postSomething', {
email: varEmail, //varEmail is a variable which holds the email
password: varPassword
},
{
headers: {
Authorization: 'Bearer ' + varToken
}
})
내 방식은 다음과 같이 요청을 설정하는 것입니다.
axios({
method: 'post', //you can set what request you want to be
url: 'https://example.com/request',
data: {id: varID},
headers: {
Authorization: 'Bearer ' + varToken
}
})
datainterceptors.request =>에서 사용하면 사용 중인 특정 호출에서 실제 신체 부분을 무시합니다. 그런 경우에는 사용하지 마십시오.
다음은 헤더와 responseType이있는 간단한 구성 예입니다.
var config = {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
responseType: 'blob'
};
axios.post('http://YOUR_URL', this.data, config)
.then((response) => {
console.log(response.data);
});
Content-Type은 'application / x-www-form-urlencoded'또는 'application / json'일 수 있으며 'application / json; charset = utf-8'도 작동 할 수 있습니다
responseType은 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'일 수 있습니다.
이 예에서 this.data는 보내려는 데이터입니다. 값 또는 배열 일 수 있습니다. (개체를 보내려면 직렬화해야 할 것입니다)
올바른 방법은 다음과 같습니다.
axios.post('url', {"body":data}, {
headers: {
'Content-Type': 'application/json'
}
}
)
기본 헤더를 초기화 할 수 있습니다 axios.defaults.headers
axios.defaults.headers = {
'Content-Type': 'application/json',
Authorization: 'myspecialpassword'
}
axios.post('https://myapi.com', { data: "hello world" })
.then(response => {
console.log('Response', response.data)
})
.catch(e => {
console.log('Error: ', e.response.data)
})
이 코드를 사용해보십시오
예제 코드에서 axios get rest API를 사용하십시오.
장착
mounted(){
var config = {
headers: {
'x-rapidapi-host': 'covid-19-coronavirus-statistics.p.rapidapi.com',
'x-rapidapi-key': '5156f83861mshd5c5731412d4c5fp18132ejsn8ae65e661a54'
}
};
axios.get('https://covid-19-coronavirus-statistics.p.rapidapi.com/v1/stats?
country=Thailand', config)
.then((response) => {
console.log(response.data);
});
}
희망은 도움입니다.