【问题标题】:Fetch POST with some headers获取带有一些标题的 POST
【发布时间】:2016-08-23 18:56:28
【问题描述】:

我想使用fetch 函数发布一些数据。我一直在使用curl 命令进行测试,目的是找到正确的响应。然后我发现我想要的命令看起来像这样:

curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml;    charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>0</BinaryState></u:SetBinaryState></s:Body></s:Envelope>' -s http://192.168.1.48:49153/upnp/control/basicevent1

我设置的fetch的参数:

fetch('http://192.168.1.48:49153/upnp/control/basicevent1', {
 method: 'POST', 
 headers: {
   'Accept' : '',
   'Content-type': text/xml; charset="utf-8",
   'SOAPACTION': "urn:Belkin:service:basicevent:1#SetBinaryState"
 },
 body: ('<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>0</BinaryState></u:SetBinaryState></s:Body></s:Envelope>')
})

我不知道问题出在 HTTP 版本还是其他标头设置中,但我收到了错误响应。

【问题讨论】:

  • text/xml; charset="utf-8" 应该用撇号括起来。
  • 你得到什么错误?

标签: javascript soap react-native fetch


【解决方案1】:

fetch api 使用promises 处理回调结果。

在你的情况下:

     fetch('http://192.168.1.48:49153/upnp/control/basicevent1', {
     method: 'POST', 
     headers: {
       'Accept' : '',
       'Content-type': 'text/xml; charset="utf-8"',
       'SOAPACTION': "urn:Belkin:service:basicevent:1#SetBinaryState"
     },
     body: ('<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>0</BinaryState></u:SetBinaryState></s:Body></s:Envelope>')
     }).catch(function(err) {
       // Error :(
     });

另外,content-type 应该是 "application/json; charset=utf-8"。

【讨论】:

  • 我收到的错误是来自 UPnP 的“无效操作”。问题是我想将我所说的 curl 命令“翻译”成 fecth 函数。我告诉过你 curl 命令按我想要的方式工作 使用问题开头的参数。所以不要现在为什么说content-type 应该是"application/json; charset=utf-8"。
【解决方案2】:

最后我修改了请求。问题在于正文中的内容时间和某些字段。

'Content-type': 'text/xml; charset=utf-8',

【讨论】:

    猜你喜欢
    • 2017-07-21
    • 2012-06-01
    • 2017-03-23
    • 2013-06-10
    • 2022-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    相关资源
    最近更新 更多