安装axios
pnpm install axios
调用Realworld接口
接口文档如下:
https://main--realworld-docs.netlify.app/docs/specs/backend-specs/endpoints
javascript">const axios = require('axios');
let token = ''
const instance = axios.create({baseURL: 'https://api.realworld.io/api',
});instance.interceptors.request.use((config) => {if (token) {const newConfig = { ...config };newConfig.headers.Authorization = `Bearer ${token}`;}return config;},(error) => Promise.reject(error)
);instance({method: 'POST',url: '/users',data:{user: {username: 'xxxxxx',email: 'xxxxxx@xxxxxx.com',password: 'xxxxxx',}}
}).then(res=>{token = res.data.user.tokenconsole.log(token);instance({url:'/user',method:'GET'}).then(response => {console.log(response.data);}).catch(error => {console.error(error);});
});