我正在使用API CALL
或POST
做Axios
宽度Fetch
方法,但在Android上发送错误的Content-Type
。
发送application/json; charset=utf-8
而不是application/json
它在iOS上运行良好,GET
方法似乎在两个平台上都运行良好。
1)我的应用程序是使用Create-React-Native App创建的,并与Expo合作
2)到目前为止我尝试过的:
Postman
中的接收数据等 - 使用Axios
或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)})
我得到的错误:
{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
如果我错过了一些主题的答案,抱歉:)
这看起来像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: {},
};
我希望这可以指出你正确的方向。