• V
 

使用模板设置请求的 URL

问题

您希望动态设置 HTTP 请求的 URL,其中只有 URL 的部分在请求之间发生变化。

解决方案

配置 HTTP 请求 节点以使用 mustache URL 模板动态生成 URL。

示例

[{"id":"41747a17.54ffd4","type":"http request","z":"c9a81b70.8abed8","name":"","method":"GET","ret":"txt","url":"https://jsonplaceholder.typicode.com/posts/{{post}}","tls":"","x":550,"y":480,"wires":[["d682318c.36823"]]},{"id":"d682318c.36823","type":"debug","z":"c9a81b70.8abed8","name":"","active":true,"console":"false","complete":"payload","x":710,"y":480,"wires":[]},{"id":"90bfea22.dd2b98","type":"inject","z":"c9a81b70.8abed8","name":"post id","topic":"","payload":"2","payloadType":"str","repeat":"","crontab":"","once":false,"x":250,"y":480,"wires":[["e67a0cc.596d4f"]]},{"id":"e67a0cc.596d4f","type":"change","z":"c9a81b70.8abed8","name":"","rules":[{"t":"set","p":"post","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":480,"wires":[["41747a17.54ffd4"]]}]

在此流中,Inject 节点发送我们希望从 API 请求的帖子的 ID。Change 节点将其更改为 msg.postHTTP 请求 节点通过替换配置的 URL 属性的 msg.post 来生成 URL,如下所示:

https://jsonplaceholder.typicode.com/posts/{{post}}

调试面板中此 API 的 JSON 输出将如下所示:

{
  "userId": 1,
  "id": 2,
  "title": "qui est esse",
  "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}

讨论

默认情况下,mustache 会转义其替换值中的任何 HTML 实体。为确保您的 URL 中不使用 HTML 转义,请使用 {{{triple}}} 大括号。