POST方法在react-native应用程序(Android)中不起作用(Fetch或Axios)

时间:2019-04-30 09:31:04

标签: reactjs react-native axios fetch content-type

我正在使用API CALLPOSTAxiosFetch的方法,但是在Android上发送了错误的Content-Type

发送application/json; charset=utf-8而不是application/json

它在iOS上运行良好,并且GET方法似乎在两种平台上均能正常工作。

1)我的应用是使用Create-React-Native应用创建的,并与Expo一起使用

2)到目前为止,我已经尝试过:

  • 我阅读了很多主题(github / stackoverflow),并做了一些更改:在标头中,发送另一种内容类型,在Postman中更改接收到的数据,等等-使用Axios或{ {1}}

示例:Fetch

我尝试过的代码(基本相同)

'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'

fetch('URL', {
  method: 'POST',
  headers: {
    'TOKEN': 'TOKEN',
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
}).then((response) => console.log(response))

我得到的错误:

axios({ method: 'post', url: LOGIN_API_URL, headers: { 'Content-type': 'application/json', 'TOKEN': 'MyTestToken', }, data: data, }).then((response) => {console.log(response.data)})

我想为两个平台(iOS和Android)发送正确的{title: "ERROR", code: "invalid ContentType", description: "Content-Type expeced: application/json, Content-Type received: application/json; charset=utf-8", success: false}

如果我错过了一些答案的主题,很抱歉:)

1 个答案:

答案 0 :(得分:1)

这看起来像是axios方面的错误(请参见https://github.com/axios/axios/issues/859) 似乎有一个可能的解决方案与是否提供data参数(https://github.com/axios/axios/issues/86)有关:

https://github.com/axios/axios/issues/86#issuecomment-405930811

const config = {
  headers: {
    accept: 'application/json',
  },
  data: {},
};

我希望这可以为您指明正确的方向。