如何使用 axios get/post?
How to get/post with axios?
我想用 axios(应该是异步的)发出一个 get 请求,然后再发出一个 post 请求。
代码如下:
async getJSONAsync()
{
await app.get('https://*****.io/index.php?/api/v2/get_case/5892', {
headers: {'Content-Type' : 'application/json'},
auth: {
username : '************',
password : '*********************'
}
}).then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
问题是 - 我应该如何存储 baseURL 和其他选项,如 headers、auth info、body(data) 以及当我返回结果时 - 如何将它与其他功能一起使用- 像 post 和以前获取请求响应的一些数据?
我在这里迷路了。请帮忙
const axios = require('axios');
const getRandomDogPicture = async () => {
const instance = axios.create({
baseURL: "https://dog.ceo/api/breeds/image/random",
timeout: 1000,
headers: {
'X-Custom-Header': 'foobar'
}
});
let result = await instance.get().then(result => result.data);
return result;
}
const func = async () => {
let ans = await getRandomDogPicture();
console.log(ans)
}
console.log(func());
我想用 axios(应该是异步的)发出一个 get 请求,然后再发出一个 post 请求。
代码如下:
async getJSONAsync()
{
await app.get('https://*****.io/index.php?/api/v2/get_case/5892', {
headers: {'Content-Type' : 'application/json'},
auth: {
username : '************',
password : '*********************'
}
}).then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
问题是 - 我应该如何存储 baseURL 和其他选项,如 headers、auth info、body(data) 以及当我返回结果时 - 如何将它与其他功能一起使用- 像 post 和以前获取请求响应的一些数据? 我在这里迷路了。请帮忙
const axios = require('axios');
const getRandomDogPicture = async () => {
const instance = axios.create({
baseURL: "https://dog.ceo/api/breeds/image/random",
timeout: 1000,
headers: {
'X-Custom-Header': 'foobar'
}
});
let result = await instance.get().then(result => result.data);
return result;
}
const func = async () => {
let ans = await getRandomDogPicture();
console.log(ans)
}
console.log(func());