1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add cookie handling to HTTP Request node

This commit is contained in:
Nick O'Leary 2017-06-05 11:33:24 +01:00
parent ef90f19eaa
commit 6c2de40dba
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 47 additions and 0 deletions

View File

@ -81,6 +81,8 @@
Must be one of <code>GET</code>, <code>PUT</code>, <code>POST</code>, <code>PATCH</code> or <code>DELETE</code>.</dd>
<dt class="optional">headers <span class="property-type">object</span></dt>
<dd>Sets the HTTP headers of the request.</dd>
<dt class="optional">cookies <span class="property-type">object</span></dt>
<dd>If set, can be used to send cookies with the request.</dd>
<dt class="optional">payload</dt>
<dd>Sent as the body of the request.</dd>
</dl>
@ -97,6 +99,8 @@
<dt>responseUrl <span class="property-type">string</span></dt>
<dd>In case any redirects occurred while processing the request, this property is the final redirected url.
Otherwise, the url of the original request.</dd>
<dt>responseCookies <span class="property-type">object</span></dt>
<dd>If the response includes cookies, this propery is an object of name/value pairs for each cookie.</dd>
</dl>
<h3>Details</h3>
<p>When configured within the node, the URL property can contain <a href="http://mustache.github.io/mustache.5.html" target="_blank">mustache-style</a> tags. These allow the
@ -104,6 +108,11 @@
<code>example.com/{{{topic}}}</code>, it will have the value of <code>msg.topic</code> automatically inserted.
Using {{{...}}} prevents mustache from escaping characters like / & etc.</p>
<p><b>Note</b>: If running behind a proxy, the standard <code>http_proxy=...</code> environment variable should be set and Node-RED restarted.</p>
<h4>Cookie handling</h4>
<p>The <code>cookies</code> property passed to the node must be an object of name/value pairs.
The value can be either a string to set the value of the cookie or it can be an
object with a single <code>value</code> property.<p>
<p>Any cookies returned by the request are passed back under the <code>responseCookies</code> property.</p>
</script>
<script type="text/javascript">

View File

@ -21,6 +21,7 @@ module.exports = function(RED) {
var urllib = require("url");
var mustache = require("mustache");
var querystring = require("querystring");
var cookie = require("cookie");
function HTTPRequest(n) {
RED.nodes.createNode(this,n);
@ -96,6 +97,28 @@ module.exports = function(RED) {
}
}
}
if (msg.cookies) {
var cookies = [];
if (opts.headers.hasOwnProperty('cookie')) {
cookies.push(opts.headers.cookie);
}
for (var name in msg.cookies) {
if (msg.cookies.hasOwnProperty(name)) {
if (msg.cookies[name] === null || msg.cookies[name].value === null) {
// This case clears a cookie for HTTP In/Response nodes.
// Ignore for this node.
} else if (typeof msg.cookies[name] === 'object') {
cookies.push(cookie.serialize(name,msg.cookies[name].value));
} else {
cookies.push(cookie.serialize(name,msg.cookies[name]));
}
}
}
if (cookies.length > 0) {
opts.headers.cookie = cookies.join("; ");
}
}
if (this.credentials && this.credentials.user) {
opts.auth = this.credentials.user+":"+(this.credentials.password||"");
}
@ -171,6 +194,20 @@ module.exports = function(RED) {
msg.responseUrl = res.responseUrl;
msg.payload = [];
if (msg.headers.hasOwnProperty('set-cookie')) {
msg.responseCookies = {};
msg.headers['set-cookie'].forEach(function(c) {
var parsedCookie = cookie.parse(c);
var eq_idx = c.indexOf('=');
var key = c.substr(0, eq_idx).trim()
parsedCookie.value = parsedCookie[key];
delete parsedCookie[key];
msg.responseCookies[key] = parsedCookie;
})
}
// msg.url = url; // revert when warning above finally removed
res.on('data',function(chunk) {
if (!Buffer.isBuffer(chunk)) {

View File

@ -31,6 +31,7 @@
"body-parser": "1.15.2",
"cheerio":"0.22.0",
"clone": "2.1.0",
"cookie": "0.3.1",
"cookie-parser": "1.4.3",
"cors":"2.8.1",
"cron":"1.2.1",