Does "HTTP In" supports query params in the format of ?query=abc

111 views
Skip to first unread message

Karthik Kannan

unread,
Nov 16, 2016, 2:37:20 PM11/16/16
to Node-RED
Hi All,

I'm trying to pass the query parameters in HTTP request. I tried two ways 

1. /test/name/:name - WORKING 
2. /test?name=:name - NOT WORKING

is this limitation in node-red?

Appreciate your ideas/comments

Julian Knight

unread,
Nov 16, 2016, 2:56:25 PM11/16/16
to Node-RED
Yes it does.

/test?name=myname&id=myid

Julian Knight

unread,
Nov 16, 2016, 2:57:57 PM11/16/16
to Node-RED
Hang on, just re-read your example and realised you were referring to something slightly different.

I don't think that should work since the thing you are referring to needs to be part of the URL path and not the query part.

Julian Knight

unread,
Nov 16, 2016, 2:59:04 PM11/16/16
to Node-RED
To get the parameters, you don't need to do that. All you need to do is pick the parameters from the msg.res object.

Karthik

unread,
Nov 16, 2016, 3:03:48 PM11/16/16
to Node-RED
you mean, I just need to define only /test in http in node and get the query params from obj like msg.res.params['name']

Julian Knight

unread,
Nov 16, 2016, 3:36:23 PM11/16/16
to Node-RED
Yes, that's right.

Karthik

unread,
Nov 16, 2016, 3:40:03 PM11/16/16
to Node-RED
well It doesn't work. TypeError: Cannot read property 'name' of undefined

Julian Knight

unread,
Nov 16, 2016, 3:47:05 PM11/16/16
to Node-RED
Here is an example that I think that should work:

[
   
{
       
"id": "c816995a.3f904",
       
"type": "http in",
       
"z": "6a32c4d1.8c37e4",
       
"name": "",
       
"url": "/test/:id",
       
"method": "get",
       
"swaggerDoc": "",
       
"x": 170,
       
"y": 100,
       
"wires": [
           
[
               
"13251aac.37577d",
               
"22afeb7d.0ec63c",
               
"392f2a46.dac136"
           
]
       
]
   
},
   
{
       
"id": "8716a2c3.18b0a8",
       
"type": "http response",
       
"z": "6a32c4d1.8c37e4",
       
"name": "",
       
"x": 970,
       
"y": 100,
       
"wires": []
   
},
   
{
       
"id": "22afeb7d.0ec63c",
       
"type": "debug",
       
"z": "6a32c4d1.8c37e4",
       
"name": "",
       
"active": false,
       
"console": "false",
       
"complete": "true",
       
"x": 392,
       
"y": 171,
       
"wires": []
   
},
   
{
       
"id": "fbaba821.205548",
       
"type": "template",
       
"z": "6a32c4d1.8c37e4",
       
"name": "",
       
"field": "payload",
       
"fieldType": "msg",
       
"format": "handlebars",
       
"syntax": "mustache",
       
"template": "<!DOCTYPE html>\n<html ng-app=\"ngApp\">\n<head>\n</head>\n<body ng-controller=\"ngCtrl\">\n    \n    <h1>Topic: {[{topic}]}</h1>\n    <pre>\n{[{payload | json }]}\n    </pre>\n\n    <script src=\"http://ajax.googleapis.com/ajax/libs/angularjs/1.4.12/angular.min.js\"></script>\n    <script>\n        var ngApp = angular.module('ngApp', []);\n        \n        ngApp.config(function($interpolateProvider) {\n            $interpolateProvider.startSymbol('{[{');\n            $interpolateProvider.endSymbol('}]}');\n        });\n        \n        ngApp.controller('ngCtrl', ['$scope', function($scope) {\n            // Need to wrap with TRY in case not valid JSON\n            $scope.payload = JSON.parse(decodeEntities('{{payload}}'));\n            $scope.topic = '{{topic}}';\n        }]);\n        \n        function decodeEntities(encodedString) {\n            var textArea = document.createElement('textarea');\n            textArea.innerHTML = encodedString;\n            return textArea.value;\n        }\n    </script>\n</body>\n</html>\n",
       
"x": 650,
       
"y": 100,
       
"wires": [
           
[
               
"8716a2c3.18b0a8"
           
]
       
]
   
},
   
{
       
"id": "13251aac.37577d",
       
"type": "function",
       
"z": "6a32c4d1.8c37e4",
       
"name": "",
       
"func": "msg.topic = msg.req.params.id;\n\nvar params = msg.req.params;\nvar query = msg.payload;\n\nmsg.payload = { \"msg.req.params\": params, \"query (msg.payload)\": query };\n\nmsg.payload = JSON.stringify(msg.payload);\n\nreturn msg;",
       
"outputs": 1,
       
"noerr": 0,
       
"x": 390,
       
"y": 100,
       
"wires": [
           
[
               
"fbaba821.205548"
           
]
       
]
   
},
   
{
       
"id": "392f2a46.dac136",
       
"type": "debug",
       
"z": "6a32c4d1.8c37e4",
       
"name": "",
       
"active": true,
       
"console": "false",
       
"complete": "true",
       
"x": 395,
       
"y": 216,
       
"wires": []
   
}
]

Karthik

unread,
Nov 16, 2016, 3:58:38 PM11/16/16
to Node-RED
Thanks Julian!! I'm just curious, In your http request, you were invoking the http request like http://xxx:xxxx/test/id. Isn't it? 

In my 1st example, I've been using the same approach. It works fine. 

instead http://xxx:xxxx/test/id, i want to use http://xxx:xxxx/test?id and define like /test?:id in "http in"

Karthik

unread,
Nov 16, 2016, 4:00:36 PM11/16/16
to Node-RED
I guess, it seems to be working with both (/ and ?)

Julian Knight

unread,
Nov 16, 2016, 4:16:58 PM11/16/16
to Node-RED
I've never tried it before. Just tried it now with that code I gave you and the following URL:

http://localhost:1880/test/fred?jim=ben

What I got back was:

Topic: f{
 
"msg.req.params": {
 
"id": "f",
 
"something": "red"
 
},
 
"query (msg.payload)": {
 
"jim": "ben"
 
}
}
 

So it clearly does work but you still don't need it as you should get the query parameters from the correct place in msg.req.params as I suggested. You can pull them out using a function node as I have or via a change node. Alternatively, you could act on them using a switch node if you don't need to keep track. But of course, ALL user input should be sanitised before using it in a flow.

Karthik

unread,
Nov 16, 2016, 4:21:09 PM11/16/16
to Node-RED
Great! Thanks Julian :)
Reply all
Reply to author
Forward
Message has been deleted
0 new messages