如何在 node-red 中使用 XML 负载 POST
How to POST with XML payload in node-red
刚开始使用 node-red,但无法将 XML 加载到 node-red 发送 post 请求所需的 javascript 对象 msg.payload到我的服务器。
我的 API 正在寻找以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<userRequest>
<authentication>
<username>foo</username>
<password>bar</password>
</authentication>
<action>doit</action>
</userRequest>
但是如果不转义 XML,我无法将它加载到对象中。而且我找不到正确的逃脱方式。
建议?
到目前为止,我已经尝试以各种方式转义 XML 以使其通过 node-red 中的 JS 解析器,但似乎没有任何效果。 Node-red 总是抱怨代码有错误。
有它输出 XML 的示例,但没有关于如何将 POST 上的 XML 有效负载发送到外部服务器的内容。
至POST XML,您需要确保将content-type
header设置为application/xml
。例如,使用函数节点:
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;
刚开始使用 node-red,但无法将 XML 加载到 node-red 发送 post 请求所需的 javascript 对象 msg.payload到我的服务器。
我的 API 正在寻找以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<userRequest>
<authentication>
<username>foo</username>
<password>bar</password>
</authentication>
<action>doit</action>
</userRequest>
但是如果不转义 XML,我无法将它加载到对象中。而且我找不到正确的逃脱方式。
建议?
到目前为止,我已经尝试以各种方式转义 XML 以使其通过 node-red 中的 JS 解析器,但似乎没有任何效果。 Node-red 总是抱怨代码有错误。
有它输出 XML 的示例,但没有关于如何将 POST 上的 XML 有效负载发送到外部服务器的内容。
至POST XML,您需要确保将content-type
header设置为application/xml
。例如,使用函数节点:
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;