node通过axios调用realworld接口

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


相关推荐

  1. node通过axios调用realworld接口

    2024-06-17 14:40:05       9 阅读
  2. 通过swagger在线地址生成前端接口调用

    2024-06-17 14:40:05       12 阅读
  3. nodejs 实现内部之间接口的相互调用

    2024-06-17 14:40:05       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 14:40:05       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 14:40:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 14:40:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 14:40:05       20 阅读

热门阅读

  1. (55)MOS管专题--->(10)MOS管的封装

    2024-06-17 14:40:05       9 阅读
  2. 为什么要把ip和mac地址绑定

    2024-06-17 14:40:05       5 阅读
  3. [深度学习]--分类问题的排查错误的流程

    2024-06-17 14:40:05       7 阅读
  4. vue页面带滚动条,打开新页面页面不置顶的问题

    2024-06-17 14:40:05       6 阅读
  5. postman工具的使用

    2024-06-17 14:40:05       6 阅读
  6. 计算机专业的未来展望

    2024-06-17 14:40:05       9 阅读
  7. 决策树算法介绍:原理与案例实现

    2024-06-17 14:40:05       8 阅读
  8. Python 爬虫 文本转语音 支持多种音色模型选择

    2024-06-17 14:40:05       11 阅读
  9. LAMP部署及应用

    2024-06-17 14:40:05       6 阅读
  10. Linux Centos7.5 开放指定端口

    2024-06-17 14:40:05       5 阅读
  11. 走的人多了,也便成了路(八)

    2024-06-17 14:40:05       8 阅读
  12. Redis Cluster 为什么不支持传统的事务模型

    2024-06-17 14:40:05       8 阅读
  13. Spring Boot 面试热点(三)

    2024-06-17 14:40:05       9 阅读
  14. Dockerfile制作能够ssh的ubuntu和centos7系统

    2024-06-17 14:40:05       6 阅读