Skip to content

Commit fc163c0

Browse files
committed
Add more http flows
1 parent bd8196d commit fc163c0

12 files changed

+185
-9
lines changed

_includes/docs-toc.html

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
<ul class="toc">
2+
<li class="toc-expander"><div>V</div></li>
23
<li class="tocheader"><span>Cookbook</span>
34
<ul>
4-
<li {% if page.url == "/index.html" %}class="active"{% endif %}><a href="/">Overview</a>
5+
<li {% if page.url == "/" %}class="active"{% endif %}><a href="/">Overview</a>
56
<li {% if page.url contains "/http/" %}class="active"{% endif %}><a href="/http/">HTTP</a>
67
</ul>
78
</li>
89
</ul>
10+
<script>
11+
$(function() {
12+
$(".toc-expander").click(function() {
13+
if ($("ul.toc").hasClass('open')) {
14+
$("ul.toc").removeClass('open');
15+
} else {
16+
$("ul.toc").addClass('open');
17+
}
18+
})
19+
});
20+
</script>

_layouts/default.html

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
color: #555;
1919
white-space:nowrap;
2020
}
21+
.feedback {
22+
float: right;
23+
border: 1px solid #aaa;
24+
border-radius: 4px;
25+
padding: 3px 8px;
26+
margin: 20px 0;
27+
font-size: 12px;
28+
}
2129
</style>
2230
<div class="docs">
2331
<div class="grid">
@@ -28,6 +36,7 @@
2836
<div class="docs-content">
2937
<h1 class="docs-title">{{ page.title }}</h1>
3038
{{ content }}
39+
<div class="feedback"><a href="https://github.com/node-red/cookbook.nodered.org/issues/new?title=Feedback%20on%20'{{page.title| xml_escape}}'">Got feedback about this page? Let us know!</a></div>
3140
</div>
3241
</div>
3342
</div>

http/access-http-request-headers.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ You want to access the HTTP headers sent in a request.
99

1010
### Solution
1111

12-
Use the `req.headers` property of the message sent by the <code class="node">HTTP In</code>
12+
Use the `msg.req.headers` property of the message sent by the <code class="node">HTTP In</code>
1313
node to access the headers.
1414

1515
#### Example
@@ -34,5 +34,5 @@ node to access the headers.
3434

3535
### Discussion
3636

37-
The `msg.headers` property is an object of key/value pairs for each request header.
37+
The `msg.req.headers` property is an object of key/value pairs for each request header.
3838
The header names are all lower-cased regardless of how they appear in the request.

http/handle-query-parameters.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ You want to access the query parameters passed to an HTTP endpoint, such as:
1111

1212
### Solution
1313

14-
Use the `req.query` property of the message sent by the <code class="node">HTTP In</code>
14+
Use the `msg.req.query` property of the message sent by the <code class="node">HTTP In</code>
1515
node to access the parameters.
1616

1717
#### Example
@@ -36,7 +36,7 @@ node to access the parameters.
3636

3737
### Discussion
3838

39-
The `req.query` property is an object of key/value pairs for each query parameter.
39+
The `msg.req.query` property is an object of key/value pairs for each query parameter.
4040

4141
In the above example, a request to `/hello-query?name=Nick&colour=blue` results in the property
4242
containing:

http/handle-url-parameters.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ For example, a single endpoint that can handle requests to both:
1818

1919
Use named path parameters in your <code class="node">HTTP In</code> node's `URL`
2020
property and then access the specific value provided in a request using the
21-
`req.params` property of the message.
21+
`msg.req.params` property of the message.
2222

2323
#### Flow
2424

@@ -49,7 +49,7 @@ path that can vary between requests.
4949
The `msg.req.params` property is an object of key/value pairs for each path parameter.
5050

5151
In the above example, the node is configured with a URL of `/hello-params/:name`,
52-
so a request to `/hello-param/Nick` results in the message property containing:
52+
so a request to `/hello-param/Nick` results in the `msg.req.params` property containing:
5353

5454
~~~json
5555
{

http/index.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ title: HTTP Recipes
1010
- [Include data captured in another flow](include-data-from-another-flow.html)
1111
- [Serve JSON content](serve-json-content.html)
1212
- [Serve a local file](serve-a-local-file.html)
13-
- Post form/JSON data
14-
- Post raw data
13+
- [Post raw data to a flow](post-raw-data-to-a-flow.html)
14+
- [Post form data to a flow](post-form-data-to-a-flow.html)
15+
- [Post JSON data to a flow](post-json-data-to-a-flow.html)
1516
- Work with cookies
1617
- Simple GET request
1718
- Set the url of a request

http/post-form-data-to-a-flow.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
layout: default
3+
title: Post form data to a flow
4+
---
5+
6+
### Problem
7+
8+
You want to post form data to a flow.
9+
10+
### Solution
11+
12+
Use the <code class="node">HTTP In</code> node to listen for POST requests that
13+
have their `Content-Type` set to `application/x-www-form-urlencoded` and access
14+
the form data as properties of `msg.payload`.
15+
16+
#### Example
17+
18+
![](/images/http/http-flow-008.png)
19+
20+
~~~json
21+
[{"id":"5b98a8ac.a46758","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-form","method":"post","swaggerDoc":"","x":120,"y":820,"wires":[["bba61009.4459f"]]},{"id":"bba61009.4459f","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 {{ payload.name }}!</h1>\n </body>\n</html>","x":290,"y":820,"wires":[["6ceb930a.93146c"]]},{"id":"6ceb930a.93146c","type":"http response","z":"3045204d.cfbae","name":"","x":430,"y":820,"wires":[]}]
22+
~~~
23+
{: .flow}
24+
25+
~~~text
26+
[~]$ curl -X POST -d "name=Nick" http://localhost:1880/hello-form
27+
<html>
28+
<head></head>
29+
<body>
30+
<h1>Hello Nick!</h1>
31+
</body>
32+
</html>
33+
~~~
34+
{: .shell}
35+
36+
### Discussion
37+
38+
HTML Forms can be used to send data from the browser back to a server. If
39+
configured to `POST` the data, the browser will encode the data held in the
40+
`<form>` using a `content-type` of `application/x-www-form-urlencoded`.
41+
42+
For example, when a form that looks like this is submitted:
43+
44+
~~~html
45+
<form action="http://localhost:1880/hello-form" method="post">
46+
<input name="name" value="Nick">
47+
<button>Say hello</button>
48+
</form>
49+
~~~
50+
51+
it results in the request:
52+
53+
~~~text
54+
POST / HTTP/1.1
55+
Host: localhost:1880
56+
Content-Type: application/x-www-form-urlencoded
57+
Content-Length: 9
58+
59+
name=Nick
60+
~~~
61+
62+
When the <code class="node">HTTP In</code> node receives such a request, it
63+
parses the body of the request and makes the form data available under
64+
`msg.payload`:
65+
66+
~~~javascript
67+
var name = msg.payload.name;
68+
~~~

http/post-json-data-to-a-flow.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
layout: default
3+
title: Post JSON data to a flow
4+
---
5+
6+
### Problem
7+
8+
You want to post JSON data to a flow.
9+
10+
### Solution
11+
12+
Use the <code class="node">HTTP In</code> node to listen for POST requests that
13+
have their `Content-Type` set to `application/json` and access the parsed JSON as
14+
properties of `msg.payload`.
15+
16+
#### Example
17+
18+
![](/images/http/http-flow-008.png)
19+
20+
~~~json
21+
[{"id":"5b98a8ac.a46758","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-form","method":"post","swaggerDoc":"","x":120,"y":820,"wires":[["bba61009.4459f"]]},{"id":"bba61009.4459f","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 {{ payload.name }}!</h1>\n </body>\n</html>","x":290,"y":820,"wires":[["6ceb930a.93146c"]]},{"id":"6ceb930a.93146c","type":"http response","z":"3045204d.cfbae","name":"","x":430,"y":820,"wires":[]}]
22+
~~~
23+
{: .flow}
24+
25+
~~~text
26+
[~]$ curl -X POST -d '{"name":"Nick"}' -H "Content-type: application/json" http://localhost:1880/hello-form
27+
<html>
28+
<head></head>
29+
<body>
30+
<h1>Hello Nick!</h1>
31+
</body>
32+
</html>
33+
~~~
34+
{: .shell}
35+
36+
### Discussion
37+
38+
When the <code class="node">HTTP In</code> node receives a request with the `Content-Type`
39+
header set to `application/json` it parses the body of the request and makes the data
40+
available under `msg.payload`:
41+
42+
~~~javascript
43+
var name = msg.payload.name;
44+
~~~

http/post-raw-data-to-a-flow.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
layout: default
3+
title: Post raw data to a flow
4+
---
5+
6+
### Problem
7+
8+
You want to raw text data to a flow.
9+
10+
### Solution
11+
12+
Use the <code class="node">HTTP In</code> node to listen for POST requests that
13+
have their `Content-Type` set to `text/plain` and access the posted data as `msg.payload`.
14+
15+
#### Example
16+
17+
![](/images/http/http-flow-009.png)
18+
19+
~~~json
20+
[{"id":"3e1c5107.c1e3ae","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-raw","method":"post","swaggerDoc":"","x":120,"y":920,"wires":[["cf679478.309868"]]},{"id":"cf679478.309868","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 {{ payload }}!</h1>\n </body>\n</html>","x":290,"y":920,"wires":[["f3c1a3f0.0c3e6"]]},{"id":"f3c1a3f0.0c3e6","type":"http response","z":"3045204d.cfbae","name":"","x":430,"y":920,"wires":[]}]
21+
~~~
22+
{: .flow}
23+
24+
~~~text
25+
[~]$ curl -X POST -d 'Nick' -H "Content-type: text/plain" http://localhost:1880/hello-raw
26+
<html>
27+
<head></head>
28+
<body>
29+
<h1>Hello Nick!</h1>
30+
</body>
31+
</html>
32+
~~~
33+
{: .shell}
34+
35+
### Discussion
36+
37+
When the <code class="node">HTTP In</code> node receives a request with the `Content-Type`
38+
header set to `text/plain` it makes the body of the available as `msg.payload`:
39+
40+
~~~javascript
41+
var name = msg.payload;
42+
~~~

images/http/http-flow-008.png

8.47 KB
Loading

images/http/http-flow-009.png

8.37 KB
Loading

images/http/http-flow-010.png

74.9 KB
Loading

0 commit comments

Comments
 (0)