Skip to content

Commit 7999f81

Browse files
committed
Add some http recipes
1 parent 12e81bb commit 7999f81

11 files changed

+164
-23
lines changed

_includes/docs-toc.html

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
<ul class="toc">
2-
<li class="tocheader"><span>Documentation</span>
3-
<ul>
4-
<li><a href="/" {% if page.url == "/index.html" %}class="active"{% endif %}>Overview</a>
5-
</ul>
6-
</li>
7-
<li class="tocheader"><span>Basics</span>
8-
<ul>
9-
</ul>
10-
</li>
11-
<li class="tocheader"><span>HTTP</span>
12-
<ul>
13-
</ul>
14-
</li>
15-
<li class="tocheader"><span>MQTT</span>
2+
<li class="tocheader"><span>Cookbook</span>
163
<ul>
4+
<li {% if page.url == "/index.html" %}class="active"{% endif %}><a href="/">Overview</a>
5+
<li {% if page.url contains "/http/" %}class="active"{% endif %}><a href="/http/">HTTP</a>
176
</ul>
187
</li>
198
</ul>

_includes/footer.html

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
<div class="links">
22
<div class="grid">
33
<div class="col-1-3">
4-
<p><a href="http://nodered.org">Node-RED</a> is a visual wiring tool for the Internet of Things.</p>
4+
<p><a href="https://nodered.org">Node-RED</a> is a visual wiring tool for the Internet of Things.</p>
55
<p>A project of the <a href="https://js.foundation/">JS Foundation</a>.</p>
66
</div>
77
<div class="col-1-6">
88
<ul>
99
<li><a href="https://github.com/node-red/node-red">GitHub</a></li>
1010
<li><a href="https://www.npmjs.com/package/node-red">npm</a></li>
11-
<li><a href="https://twitter.com/nodered">Twitter</a></li>
12-
<li><a href="https://groups.google.com/forum/#!forum/node-red">Mailing List</a></li>
13-
<li><a href="https://nodered.org/slack">Slack</a></li>
11+
<li><a href="https://nodered.org/docs">Documentation</a></li>
12+
<li><a href="https://nodered.org/docs/api">APIs</a></li>
1413
</ul>
1514
</div>
1615
<div class="col-1-6">
1716
<ul>
18-
<li><a href="https://nodered.org/blog">Blog</a></li>
1917
<li><a href="https://flows.nodered.org">Flow Library</a></li>
20-
<li><a href="https://nodered.org/docs">Documentation</a></li>
21-
<li><a href="https://nodered.org/docs/api">APIs</a></li>
22-
<li><a href="https://cookbook.nodered.org">Cookbook</a></li>
2318
<li><a href="https://nodered.org/conduct">Code of Conduct</a></li>
19+
<li><a href="https://nodered.org/brand">Brand</a></li>
20+
</ul>
21+
</div>
22+
<div class="col-1-6">
23+
<ul>
24+
<li><a href="https://nodered.org/blog">Blog</a></li>
25+
<li><a href="https://twitter.com/nodered">Twitter</a></li>
26+
<li><a href="https://groups.google.com/forum/#!forum/node-red">Mailing List</a></li>
27+
<li><a href="https://nodered.org/slack">Slack</a></li>
2428
</ul>
2529
</div>
2630
</div>

_includes/header.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<ul class="navigation">
2626
<li><a href="https://nodered.org">home</a></li>
2727
<li><a href="https://nodered.org/blog">blog</a></li>
28-
<li><a href="/docs/">documentation</a></li>
28+
<li><a href="https://nodered.org/docs/">documentation</a></li>
2929
<li class="current"><a href="https://cookbook.nodered.org">cookbook</a></li>
3030
<li><a href="https://flows.nodered.org/">flows</a></li>
3131
<li><a href="https://github.com/node-red/node-red">github</a></li>

_layouts/default.html

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
{% include header.html %}
2+
<style>
3+
.docs-content pre {
4+
max-height: 100px;
5+
overflow-y: auto;
6+
}
7+
code.node {
8+
border-radius: 6px;
9+
border: 1px solid #B68181;
10+
background: #fff0f0;
11+
color: #555;
12+
white-space:nowrap;
13+
}
14+
</style>
215
<div class="docs">
316
<div class="grid">
417
<div class="col-3-12">

http/create-an-http-endpoint.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
layout: default
3+
title: Create an HTTP Endpoint
4+
---
5+
6+
### Problem
7+
8+
You want to create an HTTP endpoint that responds to GET requests with some static
9+
content, such as an HTML page or CSS stylesheet.
10+
11+
### Solution
12+
13+
Use the <code class="node">HTTP In</code> node to listen for requests, a
14+
<code class="node">Template</code> node to include the static content, and an
15+
<code class="node">HTTP Response</code> node to reply to the request.
16+
17+
![](/images/http/http-flow-001.png)
18+
19+
[{"id":"59ff2a1.fa600d4","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello","method":"get","swaggerDoc":"","x":100,"y":80,"wires":[["54c1e70d.ab3e18"]]},{"id":"54c1e70d.ab3e18","type":"template","z":"3045204d.cfbae","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n <head></head>\n <body>\n <h1>Hello World!</h1>\n </body>\n</html>","x":250,"y":80,"wires":[["266c286f.d993d8"]]},{"id":"266c286f.d993d8","type":"http response","z":"3045204d.cfbae","name":"","x":390,"y":80,"wires":[]}]
20+
21+
### Discussion
22+
23+
The <code class="node">HTTP In</code> and <code class="node">HTTP Response</code>
24+
pair of nodes are the starting point for all HTTP endpoints you create.
25+
26+
Any flow that starts with an <code class="node">HTTP In</code> node must have a
27+
path to an <code class="node">HTTP Response</code> node otherwise requests will
28+
eventually timeout.
29+
30+
The <code class="node">HTTP Response</code> node uses the `payload` property of
31+
messages it receives as the body of the response. Other properties can be used to
32+
further customize the response - they are covered in other recipes.
33+
34+
The <code class="node">Template</code> node provides a convenient way to embed
35+
a body of content into a flow. It may be desirable to maintain such static content
36+
outside of the flow.

http/handle-query-parameters.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
layout: default
3+
title: Handle query parameters passed to an HTTP endpoint
4+
---
5+
6+
### Problem
7+
8+
You want to access the query parameters passed to an HTTP endpoint, such as:
9+
10+
http://example.com/hello-query?name=Nick
11+
12+
### Solution
13+
14+
Use the `req.query` property of the message sent by the <code class="node">HTTP In</code>
15+
node to access the parameters.
16+
17+
![](/images/http/http-flow-002.png)
18+
19+
[{"id":"b34dd1af.4cb23","type":"template","z":"3045204d.cfbae","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n <head></head>\n <body>\n <h1>Hello {{req.query.name}}!</h1>\n </body>\n</html>","x":290,"y":180,"wires":[["b828f6a6.47d708"]]},{"id":"1052941d.efad6c","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-query","method":"get","swaggerDoc":"","x":120,"y":180,"wires":[["b34dd1af.4cb23"]]},{"id":"b828f6a6.47d708","type":"http response","z":"3045204d.cfbae","name":"","x":430,"y":180,"wires":[]}]
20+
21+
### Discussion
22+
23+
The `req.query` property is an object of key/value pairs for each query parameter.
24+
25+
In the above example, a request to `/hello-query?name=Nick&colour=blue` results in the property
26+
containing:
27+
28+
{
29+
name: "Nick",
30+
colour: "blue"
31+
}
32+
33+
If there are multiple query parameters with the same name, they will be provided
34+
as an array. For example, `/hello-query?colour=blue&colour=red`:
35+
36+
{
37+
colour: ['blue','red']
38+
}

http/handle-url-parameters.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
layout: default
3+
title: Handle url parameters in an HTTP endpoint
4+
---
5+
6+
### Problem
7+
8+
You want to create a single HTTP endpoint that can handle requests where parts
9+
of the path are set per-request.
10+
11+
For example, a single endpoint that can handle requests to both:
12+
13+
http://example.com/hello-param/Nick
14+
http://example.com/hello-param/Dave
15+
16+
17+
### Solution
18+
19+
Use named path parameters in your <code class="node">HTTP In</code> node's `path`
20+
property and then access the specific value provided in a request using the
21+
`req.params` property of the message.
22+
23+
![](/images/http/http-flow-003.png)
24+
25+
[{"id":"ce53954b.31ac68","type":"http response","z":"3045204d.cfbae","name":"","x":490,"y":280,"wires":[]},{"id":"288a7c0.fd77584","type":"template","z":"3045204d.cfbae","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n <head></head>\n <body>\n <h1>Hello {{req.params.name}}!</h1>\n </body>\n</html>","x":350,"y":280,"wires":[["ce53954b.31ac68"]]},{"id":"7665c67d.899a38","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-param/:name","method":"get","swaggerDoc":"","x":150,"y":280,"wires":[["288a7c0.fd77584"]]}]
26+
27+
### Discussion
28+
29+
Named path parameters in the `path` property can be used to identify parts of the path
30+
that can vary between requests.
31+
32+
The `msg.req.params` property is an object of key/value pairs for each path parameter.
33+
34+
In the above example, the node is configured with a path of `/hello-params/:name`,
35+
so a request to `/hello-param/Nick` results in the message property containing:
36+
37+
{
38+
name: "Nick"
39+
}

http/index.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
layout: default
3+
title: HTTP Recipes
4+
---
5+
6+
- [Create an HTTP Endpoint](create-an-http-endpoint.html)
7+
- [Handle query parameters passed to an HTTP endpoint](handle-query-parameters.html)
8+
- [Handle url parameters in an HTTP endpoint](handle-url-parameters.html)
9+
- Access HTTP request headers
10+
- Include data captured in another flow
11+
- Serve JSON content
12+
- Serve a local file
13+
- Post form/JSON data
14+
- Post raw data
15+
- Work with cookies
16+
- Simple GET request
17+
- Set the url of a request
18+
- Set the url of a request using a template
19+
- Set query string parameters
20+
- Get a parsed JSON response
21+
- Get a binary response
22+
- Set a request header

images/http/http-flow-001.png

7.57 KB
Loading

images/http/http-flow-002.png

8.58 KB
Loading

images/http/http-flow-003.png

9.25 KB
Loading

0 commit comments

Comments
 (0)