javascript node-red js 中的 HTTP POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25500675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 04:35:24  来源:igfitidea点击:

HTTP POST in node-red js

javascriptjsonnode.jsnode-red

提问by ninja.stop

I want to know how to do a http POST request with a JSON data into some server with API key. I searched in http://nodered.org/docs/But they haven't written anything clearly. The documentation is very messy and not even good to understand. However, I have tried to POST a JSON data which is stringified:

我想知道如何使用 API 密钥将带有 JSON 数据的 http POST 请求发送到某个服务器。我在http://nodered.org/docs/ 中搜索过,但他们没有写清楚。文档非常混乱,甚至不好理解。但是,我尝试发布一个字符串化的 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. Now I deployed it. I am getting error : Unexpected token in API node

我将输入节点添加为 HTTP POST,然后给定 url 并将其与添加了一个调试节点的功能连接起来。现在我部署了它。我收到错误消息: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.

现在我不知道该怎么做。我不知道如何做到这一点。请帮帮我。节点红色站点中没有可用的教程。

采纳答案by kmarabet

Instead of var msg = {...you should use msg.payload = {....

而不是var msg = {...你应该使用msg.payload = {...

Because msgis a standard JSON object message passed between node-red nodes, so should not be declared using var, and its payloadproperty contains the body of the message, so when the msgis provided for the HTTP request nodethe payloadproperty is sent as the body of the request (see the info tab of the HTTP request node).

因为msg是在node-red节点之间传递的标准JSON对象消息,所以不应该用var声明,它的payload属性包含消息的正文,所以当msgHTTP请求节点提供时payload属性被发送作为请求的正文(请参阅HTTP 请求节点的信息选项卡)。

回答by knolleary

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. 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. Hopefully that will change over time, but for now you'll find the mailing list is much more responsive.

您会发现当前在堆栈溢出中的 node-red 上没有很多活动。希望这会随着时间的推移而改变,但现在您会发现邮件列表的响应速度更快。

Also, if you have specific feedback, we'd welcome it on the mailing list.

此外,如果您有具体的反馈意见,我们欢迎在邮件列表中提出。