【问题标题】:Different headers for post and get request with axios,create?使用 axios 发布和获取请求的不同标头,创建?
【发布时间】:2020-01-31 11:00:45
【问题描述】:
export default myAPI = axios.create({
  baseURL: 'myapiurl',
  // headers for post reuqest
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    Accept: 'application/json',
    'X-CSRF-TOKEN': 'o987WyyzM7ktyEVzP4dakCdIY12LprtJU8qZHs5Xs0s',
  },
  // headers for get requests
  headers: {
    'Content-Type': 'application/json',
    Accept: 'application/json',
  },
});

基本上需要根据天气发布或获取请求的条件标头。

【问题讨论】:

  • 请明确提及您面临的与此相关的问题

标签: javascript react-native asynchronous axios fetch


【解决方案1】:

您可以通过这种方式使用基于HTTP方法的不同headers:

axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

这里解释:https://github.com/axios/axios#global-axios-defaults

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2019-07-05
    • 2022-01-23
    • 2022-06-25
    • 2017-12-04
    • 2020-06-20
    • 2020-02-05
    相关资源
    最近更新 更多