使用一些标头获取POST

时间:2016-08-23 18:56:28

标签: javascript soap react-native fetch

我想使用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版本还是其他标头设置中,但我收到错误回复。

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分:0)

最后我修改了请求。问题是内容时间和身体内的一些领域。

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