简体   繁体   English

节点红色js中的HTTP POST

[英]HTTP POST in node-red js

I want to know how to do a http POST request with a JSON data into some server with API key. 我想知道如何使用API​​密钥将带有JSON数据的http POST请求放入某个服务器。 I searched in http://nodered.org/docs/ But they haven't written anything clearly. 我在http://nodered.org/docs/搜索了但他们没有写清楚任何东西。 The documentation is very messy and not even good to understand. 文档非常混乱,甚至不太好理解。 However, I have tried to POST a JSON data which is stringified: 但是,我试图POST一个字符串化的JSON数据:

{"version":"1.0.1","sensors":[{"sensor":"accel","output":[{"name":"accelert","type":"dcmotion"}]}]}

I have written the API in function node as (API is arbitary here, not original) 我已经在函数节点中编写了API(这里是API,不是原创的)

var msg = {"version":"1.0.1","sensors":[{"sensor":"accel","output":[{"name":"accelert","type":"dcmotion"}]}]}

msg.headers: {
                    'x-api-key': 'ucasdfeacceacxfAIH2L4=',
                    'content-type': 'application/json"
                }

I got this example from here : https://groups.google.com/forum/#!msg/node-red/nl9Be0dN55g/S_VYMTjOanEJ 我从这里得到了这个例子: https//groups.google.com/forum/#!msg / node-red / nl9Be0dN55g / S_VYMTjOanEJ

And I added input node as HTTP POST then given the url and connected it with function added one debug node. 我将输入节点添加为HTTP POST,然后给出url并将其与函数连接添加一个调试节点。 Now I deployed it. 现在我部署了它。 I am getting error : Unexpected token in API node 我收到错误:API节点中出现意外的令牌

Now I'm not sure how to do that. 现在我不知道该怎么做。 I am not getting how to do this. 我没有得到如何做到这一点。 Please help me out. 请帮帮我。 No tutorial available in node red site. 节点红色站点中没有可用的教程。

Instead of var msg = {... you should use msg.payload = {... . 而不是var msg = {......你应该使用msg.payload = {...

Because msg is a standard JSON object message passed between node-red nodes, so should not be declared using var, and its payload property contains the body of the message, so when the msg is provided for the HTTP request node the payload property is sent as the body of the request (see the info tab of the HTTP request node ). 因为msg是在节点 - 红色节点之间传递的标准JSON对象消息,所以不应该使用var声明,并且其payload属性包含消息的主体,因此当为HTTP请求节点提供msg时,将发送payload属性作为请求的主体(请参阅HTTP请求节点的info选项卡)。

From your subsequent question on this topic, I see you've got past the issues you were having here. 从你关于这个主题的后续问题,我看到你已经解决了你在这里遇到的问题。

To repeat the answer I gave there, the payload you want to post should be in a property called 'payload' on the object you return from the function. 要重复我在那里给出的答案,您要发布的有效负载应该位于从函数返回的对象上称为“payload”的属性中。 The documentation of the http request node describes all of that. http请求节点的文档描述了所有这些。

You will find there is not currently a lot of activity on node-red within stack overflow. 你会发现目前堆栈溢出中没有很多关于node-red的活动。 Hopefully that will change over time, but for now you'll find the mailing list is much more responsive. 希望这会随着时间的推移而改变,但是现在你会发现邮件列表的响应速度要快得多。

Also, if you have specific feedback, we'd welcome it on the mailing list. 此外,如果您有具体的反馈意见,我们欢迎您在邮件列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM