安装axios
pnpm install axios
调用Realworld接口
接口文档如下:
https://main--realworld-docs.netlify.app/docs/specs/backend-specs/endpoints
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.token console.log(token); instance({ url:'/user', method:'GET' }).then(response => { console.log(response.data); }) .catch(error => { console.error(error); }); });
还没有评论,来说两句吧...