How can I take json and encode into a URL

857 views
Skip to first unread message

Jon Murphy

unread,
Jan 4, 2017, 11:38:58 PM1/4/17
to Node-RED
I’m looking for assistance encoding a URL.  I am trying to make this msg object:

{
"selection": {
"includeAlerts": "false",
"selectionType": "registered",
"selectionMatch": "",
"includeEvents": "false",
"includeSettings": "false",
"includeRuntime": "true",
"includeSensors": "true"
}
}"
    "method": "GET",
    "headers": {"Content-Type": "application/json;charset=UTF-8",
        "Authorization": "Bearer 12345678901234567890123456789012"
    }};


into this:

msg = {"url": "https://api.ecobee.com/1/thermostat?format=json&body=%7B%22selection%22%3A%7B%22includeAlerts%22%3A%22false%22%2C%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3A%22%22%2C%22includeEvents%22%3A%22false%22%2C%22includeSettings%22%3A%22false%22%2C%22includeRuntime%22%3A%22true%22%2C%22includeSensors%22%3A%22true%22%7D%7D",
"method": "GET",
"headers": {"Content-Type": "application/json;charset=UTF-8",
"Authorization": "Bearer 12345678901234567890123456789012"};

The above is pushed into a http request node.

Is there a node-red node to help with this?  Or a javascript command?





Mark Setrem

unread,
Jan 5, 2017, 6:54:24 AM1/5/17
to Node-RED
have you looked at the javascript "encode" command?

Jon Murphy

unread,
Jan 5, 2017, 10:36:40 AM1/5/17
to Node-RED
Hi Mark!  I found similar commands but I did not locate "encode":
  • escape()     will not encode: @*/+
  • encodeURI()      will not encode: ~!@#$&*()=:/,;?+'
  • encodeURIComponent()      will not encode: ~!*()'
I'll give one of these a try.  Thank you!   Jon

Dave C-J

unread,
Jan 5, 2017, 11:08:01 AM1/5/17
to node...@googlegroups.com
any reason not to just pass in the body as the msg.payload and let the http node take care of it ?
(as per the info on the right - 
  • payload is sent as the body of the request

Dave C-J

unread,
Jan 5, 2017, 11:09:44 AM1/5/17
to node...@googlegroups.com
Ah - it's a GET request... - still may be worth a try

Jon Murphy

unread,
Jan 5, 2017, 12:14:26 PM1/5/17
to Node-RED
Mark - I did a encodeURIComponent and ended up with and [object] [object] so I added a JSON.stringify.  The stringify escapes each quote and it seems like this is not needed.  So I may remove the escape characters with a replace command.  Overall this seems to work!

    var urlBody = {
        "selection": {
            "includeAlerts": "false",
            "selectionType": "registered",
            "selectionMatch": "",
            "includeEvents": "false",
            "includeSettings": "false",
            "includeRuntime": "true",
            "includeSensors": "true"
        }
    };

    var urlBodyStr = JSON.stringify(urlBody);
    var encodedBody = encodeURIComponent(urlBodyStr);

   msg = {
        "url": "https://api.ecobee.com/1/thermostat?format=json&body=" + encodedBody,
        "method": "GET",
        headers: {
            "Content-Type": "application/json;charset=UTF-8",
            "Authorization": "Bearer " + flow.get('EcobeeAccessToken'),
        }
  

Dave - I think this is what you suggested (see below).  I’ll give it a try!

msg = {
payload”: urlBody,
"url": "https://api.ecobee.com/1/thermostat?format=json",
        "method": "GET",
        headers: {
            "Content-Type": "application/json;charset=UTF-8",
            "Authorization": "Bearer " + flow.get('EcobeeAccessToken'),
        }

Thank you!

Jon Murphy

unread,
Jan 5, 2017, 1:10:09 PM1/5/17
to Node-RED
Dave - I removed the body from the URL:
&body=%7B%22selection%22%3A%7B%22includeAlerts%22%3A%22false%22%2C%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3A%22%22%2C%22includeEvents%22%3A%22false%22%2C%22includeSettings%22%3A%22false%22%2C%22includeRuntime%22%3A%22true%22%2C%22includeSensors%22%3A%22true%22%7D%7D

and placed it in payload:
msg.payload = "%7B%22selection%22%3A%7B%22includeAlerts%22%3A%22false%22%2C%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3A%22%22%2C%22includeEvents%22%3A%22false%22%2C%22includeSettings%22%3A%22false%22%2C%22includeRuntime%22%3A%22true%22%2C%22includeSensors%22%3A%22true%22%7D%7D";

and I get this error back from the server:
{ "payload": { "status": { "code": 4, "message": "Serialization error. Request contains no body. Ensure body is provided and POST Content-Type specified correctly." } }

I also tried with:
msg.payload = {"selection":{"includeAlerts":"false","selectionType":"registered","selectionMatch":"","includeEvents":"false","includeSettings":"false","includeRuntime":"true","includeSensors":"true"}};

Dave C-J

unread,
Jan 5, 2017, 3:55:44 PM1/5/17
to node...@googlegroups.com
Shame - does the endpoint you are targeting have a POST option as well as GET ? 

Jon Murphy

unread,
Jan 5, 2017, 6:16:09 PM1/5/17
to Node-RED
yes, it does.

HTTP Method
API requests are made using the HTTP GET or POST methods, depending on the request type. Retrieving information is accomplished with a GET request whereas updating information is achieved with the POST request.
Reply all
Reply to author
Forward
0 new messages