刚开始使用node-red并且无法将XML加载到javascript对象msg.payload中,node-red需要将post请求发送到我的服务器。
我的API正在寻找以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<userRequest>
<authentication>
<username>foo</username>
<password>bar</password>
</authentication>
<action>doit</action>
</userRequest>
&#13;
但是如果没有转义XML,我就无法将其加载到对象中。我无法找到逃脱它的正确方法。
么?
到目前为止,我已尝试以各种方式逃避XML以使其以节点红色通过JS解析器,但似乎没有任何效果。 Node-red总是抱怨代码中有错误。
有一些输出XML的示例,但没有关于如何将POST上的XML有效负载发送到外部服务器的示例。
答案 0 :(得分:3)
要POST XML,您需要确保将content-type
标头设置为application/xml
。例如,使用Function节点:
msg.payload = '<?xml version="1.0" encoding="UTF-8"?><userRequest<authentication><username>foo</username><password>bar</password></authentication><action>doit</action></userRequest>';
msg.headers = {};
msg.headers['content-type'] = 'application/xml';
return msg;