2016-08-23 42 views
-1

我想使用fetch函数POST一些数据。我一直在使用curl命令进行测试,目的是找到正确的响应。后来我发现,我想要工作的命令如下:提取一些标头POST

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>') 
}) 

我不知道,如果问题出在:

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参数HTTP版本或其他标题设置,但我收到错误响应。

+0

'text/xml; charset =“utf-8”'应该用撇号包裹。 –

+0

你会得到什么错误? –

回答

0

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

'Content-type': 'text/xml; charset=utf-8', 
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"

+0

我收到的错误是来自UPnP的“无效操作”。问题是我想将我说的curl命令“翻译”为fecth函数。 我告诉过你,** curl命令的工作原理与我想要的**一样,在问题开始时使用参数。所以,现在不要说为什么你说'content-type'应该是'“application/json; charset = utf-8”'。 –