反应原生应用程序(Android)中的POST方法不起作用(Fetch或Axios)

问题描述 投票:0回答:1

我正在使用API CALLPOSTAxios宽度Fetch方法,但在Android上发送错误的Content-Type

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

它在iOS上运行良好,GET方法似乎在两个平台上都运行良好。

1)我的应用程序是使用Create-React-Native App创建的,并与Expo合作

2)到目前为止我尝试过的:

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

示例:'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)})

我得到的错误:

{title: "ERROR", code: "invalid ContentType", description: "Content-Type expeced: application/json, Content-Type received: application/json; charset=utf-8", success: false}

我想为这两个平台发送正确的content-type: 'application/json':ios和android

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

reactjs react-native axios fetch content-type
1个回答
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: {},
};

我希望这可以指出你正确的方向。

© www.soinside.com 2019 - 2024. All rights reserved.