Fetch API post XML request error

六月ゝ 毕业季﹏ 提交于 2019-12-04 02:13:08

问题


I am failing to do a fetch POST request. It returns a 400 bad request error

fetch('http://192.168.1.6:49152/ctl/RenderingControl', {
    method: 'POST',
    headers: {
        "SOAPAction": "urn:schemas-upnp-org:service:RenderingControl:1#GetVolume",
        "content-type": "text/xml; charset=utf-8"
        },
    body: '<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <s:Body><u:GetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1"><InstanceID>0</InstanceID><Channel>Master</Channel></u:GetVolume></s:Body></s:Envelope>'
    }).then(function (response) {
        console.log(response.text());
    }).catch(function (error) {
        console.log('Request failed', error);
    });

However this curl request works

echo '
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <s:Body>
  <u:GetVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1">
   <InstanceID>0</InstanceID>
   <Channel>Master</Channel>
  </u:GetVolume>
 </s:Body>
</s:Envelope>
' | curl -v -d @- \
 -H 'SOAPAction: "urn:schemas-upnp-org:service:RenderingControl:1#GetVolume"' \
 -H 'content-type: text/xml; charset="utf-8"' \
 http://192.168.1.6:49152/ctl/RenderingControl

So I know that the body and header content are correct. But where am I doing wrong with the fetch request?

来源:https://stackoverflow.com/questions/46593491/fetch-api-post-xml-request-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!