在 POST 请求中使用 react-native 中的 axios 设置标头



我在使用 axios 的反应原生时遇到了奇怪的问题,我有一个集线器,当我尝试使用 Postman 时,我需要在其中发出注册 puck 的后请求,它工作得很好,但是当我从 Ipad 模拟器尝试时,它会抛出这个响应:

{"data":{"jsonrpc":"2.0","id":"27316","error":{"code":1,"message":"Missing PLSType field","data":null}},"status":200,"headers":{"plsversion":"20190607","content-type":"application/json","server":"akka-http/10.1.8","plstype":"64","plsname":"ThinkinMiddleware","date":"Thu, 06 Feb 2020 11:01:19 GMT","content-length":"95"},"config":{"url":"http://192.168.20.229:8383","method":"post","data":"{"jsonrpc":"2.0","id":"27316","method":"crManagementExtension","params":{"mode":1,"puck":{"id":"DBC0000000fa","number":13}}}","headers":{"Accept":"application/json, text/plain, */*","Content-Type":"application/json","PLSType":32,"PLSName":"cash-559892","PLSVersion":"1.2.3.4"},"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1},"request":{"UNSENT":0,"OPENED":1,"HEADERS_RECEIVED":2,"LOADING":3,"DONE":4,"readyState":4,"status":200,"timeout":0,"withCredentials":true,"upload":{},"_aborted":false,"_hasError":false,"_method":"POST","_response":"{"jsonrpc":"2.0","id":"27316","error":{"code":1,"message":"Missing PLSType field","data":null}}","_url":"http://192.168.20.229:8383","_timedOut":false,"_trackingName":"unknown","_incrementalEvents":false,"responseHeaders":{"PLSVersion":"20190607","Content-Type":"application/json","Server":"akka-http/10.1.8","PLSType":"64","PLSName":"ThinkinMiddleware","Date":"Thu, 06 Feb 2020 11:01:19 GMT","Content-Length":"95"},"_requestId":null,"_headers":{"accept":"application/json, text/plain, */*","content-type":"application/json","plstype":"32","plsname":"cash-559892","plsversion":"1.2.3.4"},"_responseType":"","_sent":true,"_lowerCaseResponseHeaders":{"plsversion":"20190607","content-type":"application/json","server":"akka-http/10.1.8","plstype":"64","plsname":"ThinkinMiddleware","date":"Thu, 06 Feb 2020 11:01:19 GMT","content-length":"95"},"_subscriptions":[],"responseURL":"http://192.168.20.229:8383/"}}

我在此日志中看到的是 PLSType 字段丢失,这意味着未设置标头 PLSType。

但是当我尝试使用邮递员时,我得到以下响应:

{
"jsonrpc": "2.0",
"id": 1234,
"result": {
"status": 10,
"puck": {
"battery": 100,
"colors": null,
"location": null
}
}
}

这是执行的函数:

axios.post(
"http://192.168.20.229:8383",
{
jsonrpc: "2.0",
id: "27316",
method: "crManagementExtension",
params: {mode: 1, puck: {id: "DBC0000000fa", number: 13}},
},
{
headers: {
"Content-Type": "application/json",
PLSType: 32,
PLSName: "cash-559892",
PLSVersion: "1.2.3.4",
}
},
).then(response => {
console.log('response sukses')
console.log(JSON.stringify(response));
// return resolve(response);
})
.catch(error => {
console.log('error while registering puck sukses')
console.log(error);
// reject(error);
});

我在 React JS 中尝试了完全相同的功能,它可以工作。

尝试在标题中添加它。

'Accept-Encoding': 'gzip, deflate, br'

我遇到了完全相同的问题。即使使用fetch也不会让事情变得更容易。

我也用Postman尝试过,对我来说,看起来标题是从Android/iPad/iOS默认http客户端传递错误的。

检查request对象不会告诉您与 Postman 的请求相比请求有任何差异。

今天晚上,我将通过检查发送到服务器的实际请求来验证我的论文。

只是被其他人偶然发现。我收到HTTP错误406。由于客户端正在做一些奇怪的事情,因此其他客户端不会这样做。

它既不是您的Android权限的cleartext标志,也不是gzipping AFAIK。

最新更新