Creating something similar to ASP.NET Web API in NODE-RED.

446 views
Skip to first unread message

Yogesh

unread,
Jan 4, 2016, 2:46:36 PM1/4/16
to Node-RED
I am a newbie to the node-red world. I am currently working on a project where I want to expose something similar to a Web API in ASP.NET in my Node-Red flow. I know I could use the Input HTTP Node to use it like a Web Server, but beyond that I am not sure how best to wire it to something similar to a method that can handle GET, PUT requests and expect some input parameters. I have done this using Express using node.js, but not sure how to translate similar functionality in NODE-RED. Any suggestions are greatly appreciated.

Thanks,
Yogesh

Dave C-J

unread,
Jan 4, 2016, 2:52:10 PM1/4/16
to node...@googlegroups.com
This may be slightly more complex than you need - as it also includes live updates over websockets...
but

Yogesh

unread,
Jan 4, 2016, 5:17:14 PM1/4/16
to Node-RED
Thanks Dave for your quick response. Although I may not need the web sockets part of it, since I don't need any real time status updates. All I want to do is able to send a json object back when they call a specific api on a get request and able to receive json object/binary data if possible when the call is a post/put based API call, if that made sense. 

In the Get request in your flow, I see you are sending an html using the template node, does it automatically set the content-type header and the http status in that case, since i want to be able to return a JSON object instead of an html.

Thanks,
Yogesh

Dave C-J

unread,
Jan 4, 2016, 5:30:55 PM1/4/16
to node...@googlegroups.com
Hi

if you look at the info page for the http response object - you can pass in msg.headers - which can be an object with whatever headers you required to set - so  you would need to set msg.header to  {"Content-Type":"application/json"}

Nicholas O'Leary

unread,
Jan 4, 2016, 5:46:55 PM1/4/16
to Node-RED Mailing List
But if msg.payload is a JavaScript object, and msg.headers['content-type'] isn't set, then we assume you wanted to send it as JSON and set the header for you... we're nice like that.



Nick

On 4 January 2016 at 22:30, Dave C-J <dce...@gmail.com> wrote:
Hi

if you look at the info page for the http response object - you can pass in msg.headers - which can be an object with whatever headers you required to set - so  you would need to set msg.header to  {"Content-Type":"application/json"}

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages