Skip to content

Commit ec14dad

Browse files
committed
Add cookies recipe
1 parent fc163c0 commit ec14dad

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

http/index.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ layout: default
33
title: HTTP Recipes
44
---
55

6+
#### HTTP endpoints
7+
68
- [Create an HTTP Endpoint](create-an-http-endpoint.html)
79
- [Handle query parameters passed to an HTTP endpoint](handle-query-parameters.html)
810
- [Handle url parameters in an HTTP endpoint](handle-url-parameters.html)
@@ -13,7 +15,9 @@ title: HTTP Recipes
1315
- [Post raw data to a flow](post-raw-data-to-a-flow.html)
1416
- [Post form data to a flow](post-form-data-to-a-flow.html)
1517
- [Post JSON data to a flow](post-json-data-to-a-flow.html)
16-
- Work with cookies
18+
- [Work with cookies](work-with-cookies.html)
19+
20+
#### HTTP requests
1721
- Simple GET request
1822
- Set the url of a request
1923
- Set the url of a request using a template

http/work-with-cookies.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
layout: default
3+
title: Work with cookies
4+
---
5+
6+
### Problem
7+
8+
You want to create an HTTP flow that uses cookies.
9+
10+
### Solution
11+
12+
The messages sent by the <code class="node">HTTP In</code> node include the
13+
`msg.req.cookies` property that lists the cookies set on the current request.
14+
15+
The <code class="node">HTTP Response</code> node will use the `msg.cookies` property
16+
in order to set or clear cookies.
17+
18+
#### Example
19+
20+
![](/images/http/http-flow-010.png)
21+
22+
~~~json
23+
[{"id":"c362b989.954ae8","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-cookie","method":"get","swaggerDoc":"","x":130,"y":1020,"wires":[["21ddf71f.d00518"]]},{"id":"21ddf71f.d00518","type":"function","z":"3045204d.cfbae","name":"Format cookies","func":"msg.payload = JSON.stringify(msg.req.cookies,null,4);\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":1020,"wires":[["f3aa98c1.befc18"]]},{"id":"f3aa98c1.befc18","type":"template","z":"3045204d.cfbae","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n <head></head>\n <body>\n <h1>Cookies</h1>\n <p></p><a href=\"hello-cookie/add\">Add a cookie</a> &bull; <a href=\"hello-cookie/clear\">Clear cookies</a></p>\n <pre>{{ payload }}</pre>\n </body>\n</html>","x":530,"y":1020,"wires":[["f52e2880.180968"]]},{"id":"f52e2880.180968","type":"http response","z":"3045204d.cfbae","name":"","x":750,"y":1020,"wires":[]},{"id":"9a2a9a4.0fc0768","type":"change","z":"3045204d.cfbae","name":"Redirect to /hello-cookie","rules":[{"t":"set","p":"statusCode","pt":"msg","to":"302","tot":"num"},{"t":"set","p":"headers","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"headers.location","pt":"msg","to":"/hello-cookie","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":1080,"wires":[["f52e2880.180968"]]},{"id":"afefb90.53dcf48","type":"function","z":"3045204d.cfbae","name":"Add a cookie","func":"msg.cookies = { };\nmsg.cookies[\"demo-\"+(Math.floor(Math.random()*1000))] = Date.now();\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":1060,"wires":[["9a2a9a4.0fc0768"]]},{"id":"d5205a2c.db9018","type":"function","z":"3045204d.cfbae","name":"Clear cookies","func":"// Find demo cookies and clear them\nvar cookieNames = Object.keys(msg.req.cookies).filter(function(cookieName) { return /^demo-/.test(cookieName);});\nmsg.cookies = {};\n\ncookieNames.forEach(function(cookieName) {\n msg.cookies[cookieName] = null;\n});\n\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":1100,"wires":[["9a2a9a4.0fc0768"]]},{"id":"fda60c66.04975","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-cookie/add","method":"get","swaggerDoc":"","x":140,"y":1060,"wires":[["afefb90.53dcf48"]]},{"id":"35285a76.1f8636","type":"http in","z":"3045204d.cfbae","name":"","url":"/hello-cookie/clear","method":"get","swaggerDoc":"","x":140,"y":1100,"wires":[["d5205a2c.db9018"]]}]
24+
~~~
25+
{: .flow}
26+
27+
This example provides three HTTP endpoints:
28+
29+
- `/hello-cookie` returns a page that lists the cookies currently set
30+
- `/hello-cookie/add` adds a new cookie and redirects back to `/hello-cookie`
31+
- `/hello-cookie/clear` clears all cookies created by the example and redirects back to `/hello-cookie`
32+
33+
### Discussion
34+
35+
The `msg.req.cookies` property is an object of key/value pairs containing the cookies
36+
set on the current request.
37+
38+
~~~javascript
39+
var mySessionId = msg.req.cookies['sessionId'];
40+
~~~
41+
42+
In order to set a cookie in the response, the `msg.cookies` property should be set
43+
to a similar key/value object.
44+
45+
The value can be either a string to set the value of the cookie with default
46+
options, or it can be an object of options.
47+
48+
The following example sets two cookies - one called `name` with a value of `Nick`, the other called `session` with a value of `1234` and an expiry set to 15 minutes.
49+
50+
~~~javascript
51+
msg.cookies = {
52+
name: 'nick',
53+
session: {
54+
value: '1234',
55+
maxAge: 900000
56+
}
57+
}
58+
~~~
59+
60+
The valid options include:
61+
62+
- `domain` - (String) domain name for the cookie
63+
- `expires` - (Date) expiry date in GMT. If not specified or set to 0, creates a session cookie
64+
- `maxAge` - (String) expiry date as relative to the current time in milliseconds
65+
- `path` - (String) path for the cookie. Defaults to /
66+
- `value` - (String) the value to use for the cookie
67+
68+
To delete a cookie, set its value to null.

0 commit comments

Comments
 (0)