2016-08-23 64 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」'。 –