Axios发布请求,标头无法正常工作

时间:2017-09-25 11:38:55

标签: react-native axios

我已使用while read l;do sshpass -p 'blabla' ssh y@x.x.x.x ip proxy access add dst-host ="$l" action = deny comment ='list' done < list.txt axios中调用网络请求,但它返回 500响应代码

注意:它在postman客户端工作得很好。

授权令牌:

  

承载eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6Ly9hcHBzZGF0YTIuY2xvdWRhcHAubmV0L2p3dC1kZW1vL3B1YmxpYy9hcGkvbG9naW4iLCJpYXQiOjE1MDYzMzkwNDgsImV4cCI6MTY2MTg1OTA0OCwibmJmIjoxNTA2MzM5MDQ4LCJqdGkiOiJHUWt5RVlwck5GSDBHekd4In0.JqdyAEkEN_D3M2WbqcQwIwghk6iajFjxi9g854akjB8

react-Native

从try catch

登录
const token = 'Bearer '.concat(this.state.token);

var headers = {
  'Content-Type': 'application/json',
  'Authorization': token
}

axios.post('http://appsdata2.cloudapp.net/jwt-demo/public/api/profile',
  null,
  headers)
.then((response ) => {
  console.log("reactNativeDemo","response get details:"+response.data);
})
.catch((error) => {
  console.log("axios error:",error);
});

2 个答案:

答案 0 :(得分:7)

Axios post方法语法如下: axios.post(url[, data[, config]])

所以你应该这样做:

axios.post('http://appsdata2.cloudapp.net/jwt-demo/public/api/profile',
null,
{headers: headers})
.then((response ) => {
  console.log("reactNativeDemo","response get details:"+response.data);
})
.catch((error) => {
  console.log("axios error:",error);
});

我在https://npm.runkit.com/axios尝试了这个并且效果很好 Please check here

答案 1 :(得分:1)

我也遇到了问题。如果您想尝试这个,如果这可以解决您的问题。

注意:这是配置您的global options。如果这适用于您,请尝试创建instances

const setAxiosToken = (token) => {
  axios.defaults.headers.common['Authorization'] = `Bearer ${token}`
  axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8'
}
setAxiosToken(YOUR_TOKEN)
axios.post('http://appsdata2.cloudapp.net/jwt-demo/public/api/profile')