Skip to content

Latest commit

 

History

History
74 lines (48 loc) · 33.6 KB

API_PRACTICAL.md

File metadata and controls

74 lines (48 loc) · 33.6 KB

Quick links : Home - Part 1 - Part 2 - Part 3 - Part 4 - Part 5


Part 2 - Local Node-RED - Web and REST APIs - Calling an API from Node-RED - Implementing an API in Node-RED - API Practical


API Practical

In this section you will need to create an API that supports both GET and POST methods and also the client functionality to test the API.

The API will display time in a given location

GET API

This method will return the time at the requested city. Options will allow the caller to specify the format of the response and also if the output is in 12H or 24H format.

  • Method : GET
  • URL : /time/at/[city or server] where city will be one of London, Washington, Singapore or Sydney. If server is specified then the time at the server location (local time) is provided.
  • query parameters :
    • format will have the value long or short. If it is not specified then short if the default behaviour
    • timeFormat will have the value 24H or 12H. If it is not specified then 24H is the default behaviour

The API will use the Accept header to return either JSON (application/json) or plain text (text/plain). Ensure the correct header is set for the format of the data returned to the client. If no type is specified or an unsupported/invalid format is requested, say application/xml, then it should be ignored and JSON should be returned.

Assuming the time is 1:47PM, for short format the output should be:

  • text : "13:47"
  • json : { "time" : "13:47"}

for long format the output should be:

  • text : "Time at [city or server] is 13:37"
  • json: { "Location" : [city or server], "time" : "13:47" }

The value for 12H should be 1:47pm and for 24H 13:47.

The aim of this practical is to get the API implementation correct rather than the correct functionality of the API, so you don't need to worry about daylight savings time when calculating time differences between cities, just use the time differences that exist today.

The status code should be set to reflect the output, 200 if everything worked OK. Refer to the status codes to get an appropriate code if the city or server parameter is not set or is not a valid value. If any of the query parameters contain invalid vales they should be ignored and the default value used.

POST API

  • Method : POST
  • URL : /time

The post API should have exactly the same functionality as the GET API, but this time there are no parameters or query parameters. All input parameters are provided in the body to the request as JSON:

{ "location" : "[city or server]", "format" : "[long | short]", "timeFormat" : "[12H|24H]" }

format and time-format properties are optional

The response should be the same as the GET API

Testing the APIs

Create a number of flows to test both the GET and POST versions of your API, use http://localhost:1880 as the server address to call local APIs. You should test valid options and also invalid options to ensure your APIs meet the above specifications.

Sample Solution

The sample solution below uses only standard nodes found in the pallet after a standard Node-RED install and doesn't use the function node:

[{"id":"f58d7901.32cb2","type":"http in","z":"78e6a193.5e7df8","name":"","url":"/time/at/:location","method":"get","upload":false,"swaggerDoc":"","x":220,"y":1260,"wires":[["48c0391d.9225b"]]},{"id":"1ca6b768.d26409","type":"http in","z":"78e6a193.5e7df8","name":"","url":"/time","method":"post","upload":false,"swaggerDoc":"","x":190,"y":1480,"wires":[["a39427fc.b12bc8"]]},{"id":"e7f69e95.a8a94","type":"change","z":"78e6a193.5e7df8","name":"calc time at location + time format","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t   $timeOffsets := {\t       \"london\" : 1 * 3600000,\t       \"singapore\" : 8 * 3600000,\t       \"sydney\": 10 * 3600000,\t       \"washington\": -4 * 3600000,\t       \"server\" : 0\t   };\t   $time := $toMillis($now())+$lookup(\t       $timeOffsets,\t       req.params.location\t   );\t   {\t       \"location\" : $uppercase(\t           $substring(req.params.location, 0, 1)\t       \t       ) & $lowercase(\t           $substring(req.params.location, 1)\t       \t       ),\t       \"time\" : (req.query.timeFormat = \"12H\") ? $fromMillis($time,'[h]:[m][P]') : $fromMillis($time,'[H]:[m]')\t   \t   }\t\t\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":1260,"wires":[["f4f86d9.13ddc1"]]},{"id":"48c0391d.9225b","type":"switch","z":"78e6a193.5e7df8","name":"valid location","property":"req.params.location","propertyType":"msg","rules":[{"t":"eq","v":"london","vt":"str"},{"t":"eq","v":"washington","vt":"str"},{"t":"eq","v":"singapore","vt":"str"},{"t":"eq","v":"sydney","vt":"str"},{"t":"eq","v":"server","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":6,"x":430,"y":1260,"wires":[["e7f69e95.a8a94"],["e7f69e95.a8a94"],["e7f69e95.a8a94"],["e7f69e95.a8a94"],["e7f69e95.a8a94"],["a9b7b68.a0bc348"]]},{"id":"deacbe70.1c034","type":"http response","z":"78e6a193.5e7df8","name":"","statusCode":"","headers":{},"x":1710,"y":1280,"wires":[]},{"id":"a9b7b68.a0bc348","type":"change","z":"78e6a193.5e7df8","name":"invalid input","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"},{"t":"set","p":"statusCode","pt":"msg","to":"400","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":1300,"wires":[["deacbe70.1c034"]]},{"id":"f4f86d9.13ddc1","type":"switch","z":"78e6a193.5e7df8","name":"output format","property":"req.headers.accept","propertyType":"msg","rules":[{"t":"eq","v":"plain/text","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":980,"y":1260,"wires":[["b4b503af.18034"],["8c2d3bae.81f478"]],"outputLabels":["text","JSON"]},{"id":"b4b503af.18034","type":"switch","z":"78e6a193.5e7df8","name":"short ot long text","property":"req.query.format","propertyType":"msg","rules":[{"t":"eq","v":"long","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1190,"y":1240,"wires":[["235815b8.915072"],["1c34cb1f.313c55"]]},{"id":"8c2d3bae.81f478","type":"switch","z":"78e6a193.5e7df8","name":"short ot long JSON","property":"req.query.format","propertyType":"msg","rules":[{"t":"eq","v":"long","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1190,"y":1280,"wires":[["deacbe70.1c034"],["d30cd0f3.90e648"]]},{"id":"235815b8.915072","type":"template","z":"78e6a193.5e7df8","name":"long text","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Time at {{payload.location}} is {{payload.time}}","output":"str","x":1380,"y":1200,"wires":[["cc27d696.689498"]]},{"id":"1c34cb1f.313c55","type":"template","z":"78e6a193.5e7df8","name":"short text","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.time}}","output":"str","x":1380,"y":1240,"wires":[["cc27d696.689498"]]},{"id":"d30cd0f3.90e648","type":"change","z":"78e6a193.5e7df8","name":"JSON short","rules":[{"t":"delete","p":"payload.location","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1390,"y":1320,"wires":[["deacbe70.1c034"]]},{"id":"a39427fc.b12bc8","type":"switch","z":"78e6a193.5e7df8","name":"valid location","property":"payload.location","propertyType":"msg","rules":[{"t":"eq","v":"london","vt":"str"},{"t":"eq","v":"washington","vt":"str"},{"t":"eq","v":"singapore","vt":"str"},{"t":"eq","v":"sydney","vt":"str"},{"t":"eq","v":"server","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":6,"x":430,"y":1480,"wires":[["a3d14324.7a0ed"],["a3d14324.7a0ed"],["a3d14324.7a0ed"],["a3d14324.7a0ed"],["a3d14324.7a0ed"],["6bf09b7c.64dc4c"]]},{"id":"a098c7ba.0d79e8","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2040,"wires":[["cf673a6b.95029"]]},{"id":"eceabdcd.e2cec","type":"inject","z":"78e6a193.5e7df8","name":"OK request defaults Sydney","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":2040,"wires":[["5ae14857.9bdb88"]]},{"id":"cf673a6b.95029","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2040,"wires":[]},{"id":"7c7b72fb.4ea774","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2400,"wires":[["e1de09f9.73dbc8"]]},{"id":"793ec7c0.d76598","type":"change","z":"78e6a193.5e7df8","name":"no location","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"long\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":2400,"wires":[["7c7b72fb.4ea774"]]},{"id":"730f0ab9.87f884","type":"inject","z":"78e6a193.5e7df8","name":"Invalid location","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":2400,"wires":[["793ec7c0.d76598"]]},{"id":"e1de09f9.73dbc8","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2400,"wires":[]},{"id":"ed605a01.b863a8","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2080,"wires":[["c6c0ae61.1bc358"]]},{"id":"eb2011a8.bbd788","type":"change","z":"78e6a193.5e7df8","name":"Server, short, 12, text","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"location\":\"server\",\"format\":\"short\",\"timeFormat\":\"12H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":2080,"wires":[["ed605a01.b863a8"]]},{"id":"c5c376d2.ef87b","type":"inject","z":"78e6a193.5e7df8","name":"OK request London","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":2080,"wires":[["eb2011a8.bbd788"]]},{"id":"c6c0ae61.1bc358","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2080,"wires":[]},{"id":"a8f90924.3eaaf","type":"comment","z":"78e6a193.5e7df8","name":"Test POST API","info":"","x":200,"y":2000,"wires":[]},{"id":"a3d14324.7a0ed","type":"change","z":"78e6a193.5e7df8","name":"calc time at location + time format","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t   $timeOffsets := {\t       \"london\" : 1 * 3600000,\t       \"singapore\" : 8 * 3600000,\t       \"sydney\": 10 * 3600000,\t       \"washington\": -4 * 3600000,\t       \"server\" : 0\t   };\t   $time := $toMillis($now())+$lookup(\t       $timeOffsets,\t       payload.location\t   );\t   {\t       \"location\" : $uppercase(\t           $substring(payload.location, 0, 1)\t       ) & $lowercase(\t           $substring(payload.location, 1)\t       ),\t       \"time\" : (payload.timeFormat = \"12H\") ? $fromMillis($time,'[h]:[m][P]') : $fromMillis($time,'[H]:[m]'),\t       \"format\" : payload.format\t   }\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":1460,"wires":[["39a41f93.a705b"]]},{"id":"6bf09b7c.64dc4c","type":"change","z":"78e6a193.5e7df8","name":"invalid input","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"},{"t":"set","p":"statusCode","pt":"msg","to":"400","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":1520,"wires":[["554e2fdf.25e3b8"]]},{"id":"39a41f93.a705b","type":"switch","z":"78e6a193.5e7df8","name":"output format","property":"req.headers.accept","propertyType":"msg","rules":[{"t":"eq","v":"plain/text","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":960,"y":1460,"wires":[["7baae20.faa659c"],["a7a2d851.ade9d8"]],"outputLabels":["text","JSON"]},{"id":"7baae20.faa659c","type":"switch","z":"78e6a193.5e7df8","name":"short ot long text","property":"payload.format","propertyType":"msg","rules":[{"t":"eq","v":"long","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1150,"y":1440,"wires":[["b0b74552.36f15"],["f2d13eed.326638"]]},{"id":"a7a2d851.ade9d8","type":"switch","z":"78e6a193.5e7df8","name":"short ot long JSON","property":"payload.format","propertyType":"msg","rules":[{"t":"eq","v":"long","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1150,"y":1480,"wires":[["45de34bf.212f7c"],["aa9aba00.ef1318"]]},{"id":"b0b74552.36f15","type":"template","z":"78e6a193.5e7df8","name":"long text","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Time at {{payload.location}} is {{payload.time}}","output":"str","x":1340,"y":1400,"wires":[["bcb7dd9d.b00048"]]},{"id":"f2d13eed.326638","type":"template","z":"78e6a193.5e7df8","name":"short text","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.time}}","output":"str","x":1340,"y":1440,"wires":[["bcb7dd9d.b00048"]]},{"id":"aa9aba00.ef1318","type":"change","z":"78e6a193.5e7df8","name":"JSON short","rules":[{"t":"delete","p":"payload.location","pt":"msg"},{"t":"delete","p":"payload.format","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1350,"y":1520,"wires":[["554e2fdf.25e3b8"]]},{"id":"554e2fdf.25e3b8","type":"http response","z":"78e6a193.5e7df8","name":"","statusCode":"","headers":{},"x":1690,"y":1480,"wires":[]},{"id":"20e3fc9e.f9c70c","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2120,"wires":[["ff59fec9.8b899"]]},{"id":"4e992357.06f044","type":"change","z":"78e6a193.5e7df8","name":"washington, long, 12 text","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"location\":\"washington\",\"format\":\"long\",\"timeFormat\":\"12H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":2120,"wires":[["20e3fc9e.f9c70c"]]},{"id":"63e3016b.27f798","type":"inject","z":"78e6a193.5e7df8","name":"OK request London","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":2120,"wires":[["4e992357.06f044"]]},{"id":"ff59fec9.8b899","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2120,"wires":[]},{"id":"4fddd0de.5d168","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2240,"wires":[["9454189a.11c9"]]},{"id":"75751256.72a184","type":"change","z":"78e6a193.5e7df8","name":"Sydney, short 24H, JSON","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"location\":\"sydney\",\"format\":\"short\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":2240,"wires":[["4fddd0de.5d168"]]},{"id":"f62f27f5.7c97c8","type":"inject","z":"78e6a193.5e7df8","name":"OK request London","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":2240,"wires":[["75751256.72a184"]]},{"id":"9454189a.11c9","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2240,"wires":[]},{"id":"1fed827c.1ed546","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2280,"wires":[["735ccb9d.40045c"]]},{"id":"2f29636.642101c","type":"change","z":"78e6a193.5e7df8","name":"London, long, 24H, JSON","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"location\":\"london\",\"format\":\"long\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":2280,"wires":[["1fed827c.1ed546"]]},{"id":"1c91738c.b7fa1c","type":"inject","z":"78e6a193.5e7df8","name":"OK request London","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":2280,"wires":[["2f29636.642101c"]]},{"id":"735ccb9d.40045c","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2280,"wires":[]},{"id":"53df4066.f5bca8","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2320,"wires":[["e2765f65.38b8a8"]]},{"id":"4d9d1242.b710bc","type":"change","z":"78e6a193.5e7df8","name":"Singapore, short, 12H, JSON","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"location\":\"singapore\",\"format\":\"short\",\"timeFormat\":\"12H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2320,"wires":[["53df4066.f5bca8"]]},{"id":"9b34801e.efe45","type":"inject","z":"78e6a193.5e7df8","name":"OK request London","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":2320,"wires":[["4d9d1242.b710bc"]]},{"id":"e2765f65.38b8a8","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2320,"wires":[]},{"id":"42015d72.59445c","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2160,"wires":[["6c34a0cc.517478"]]},{"id":"9ac6cde2.993058","type":"change","z":"78e6a193.5e7df8","name":"london, short, 24, text","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"location\":\"london\",\"format\":\"short\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":2160,"wires":[["42015d72.59445c"]]},{"id":"d7460a86.aaf41","type":"inject","z":"78e6a193.5e7df8","name":"OK request London","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":2160,"wires":[["9ac6cde2.993058"]]},{"id":"6c34a0cc.517478","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2160,"wires":[]},{"id":"3e4630ee.d42f5","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2200,"wires":[["ee9c2e90.f0eb7"]]},{"id":"bbc3fad3.ed35a","type":"change","z":"78e6a193.5e7df8","name":"Singapore, long, 24H, text","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"location\":\"singapore\",\"format\":\"long\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":2200,"wires":[["3e4630ee.d42f5"]]},{"id":"61350a5.19e0674","type":"inject","z":"78e6a193.5e7df8","name":"OK request London","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":2200,"wires":[["bbc3fad3.ed35a"]]},{"id":"ee9c2e90.f0eb7","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2200,"wires":[]},{"id":"bf0d9c1a.52575","type":"http request","z":"78e6a193.5e7df8","name":"","method":"POST","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":2360,"wires":[["52851b91.3075b4"]]},{"id":"e8f246fa.7c6e9","type":"change","z":"78e6a193.5e7df8","name":"Washington, long, 12H, JSON","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"location\":\"washington\",\"format\":\"long\",\"timeFormat\":\"12H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":2360,"wires":[["bf0d9c1a.52575"]]},{"id":"c3a9a5f2.8e20e8","type":"inject","z":"78e6a193.5e7df8","name":"OK request London","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":2360,"wires":[["e8f246fa.7c6e9"]]},{"id":"52851b91.3075b4","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":2360,"wires":[]},{"id":"5ae14857.9bdb88","type":"change","z":"78e6a193.5e7df8","name":"Sydney","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"location\":\"sydney\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":2040,"wires":[["a098c7ba.0d79e8"]]},{"id":"bcb7dd9d.b00048","type":"change","z":"78e6a193.5e7df8","name":"","rules":[{"t":"set","p":"headers","pt":"msg","to":"{\"Content-Type\":\"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1510,"y":1420,"wires":[["554e2fdf.25e3b8"]]},{"id":"cc27d696.689498","type":"change","z":"78e6a193.5e7df8","name":"","rules":[{"t":"set","p":"headers","pt":"msg","to":"{\"Content-Type\" : \"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":1550,"y":1220,"wires":[["deacbe70.1c034"]]},{"id":"45de34bf.212f7c","type":"change","z":"78e6a193.5e7df8","name":"JSON long","rules":[{"t":"delete","p":"payload.format","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1350,"y":1480,"wires":[["554e2fdf.25e3b8"]]},{"id":"6f14d963.ae6088","type":"http request","z":"78e6a193.5e7df8","name":"GET Sydney","method":"GET","ret":"txt","paytoqs":false,"url":"http://localhost:1880/time/at/sydney","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":1600,"wires":[["5eb40a70.0b9b3c"]]},{"id":"9b714ef7.e71248","type":"inject","z":"78e6a193.5e7df8","name":"OK request defaults","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1600,"wires":[["6f14d963.ae6088"]]},{"id":"5eb40a70.0b9b3c","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1600,"wires":[]},{"id":"4649269d.dcd1c8","type":"http request","z":"78e6a193.5e7df8","name":"GET Paris","method":"GET","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time/at/paris","tls":"","persist":false,"proxy":"","authType":"","x":760,"y":1960,"wires":[["54f7088b.64f968"]]},{"id":"9f15930a.af58c8","type":"change","z":"78e6a193.5e7df8","name":"no location","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"long\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":1960,"wires":[["4649269d.dcd1c8"]]},{"id":"69f83b95.5cfa24","type":"inject","z":"78e6a193.5e7df8","name":"Invalid location","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":1960,"wires":[["9f15930a.af58c8"]]},{"id":"54f7088b.64f968","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1960,"wires":[]},{"id":"9b6dc02f.686f2","type":"http request","z":"78e6a193.5e7df8","name":"GET Server","method":"GET","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time/at/server","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":1640,"wires":[["527de02f.7a7fb8"]]},{"id":"9ca85600.a1fe28","type":"change","z":"78e6a193.5e7df8","name":"short, 12, text","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"short\",\"timeFormat\":\"12H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":1640,"wires":[["9b6dc02f.686f2"]]},{"id":"f022e64f.3a1768","type":"inject","z":"78e6a193.5e7df8","name":"OK request","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1640,"wires":[["9ca85600.a1fe28"]]},{"id":"527de02f.7a7fb8","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1640,"wires":[]},{"id":"99cdf757.9bb2c","type":"comment","z":"78e6a193.5e7df8","name":"Test GET API","info":"","x":190,"y":1560,"wires":[]},{"id":"75f780d8.ada258","type":"http request","z":"78e6a193.5e7df8","name":"GET Washington","method":"GET","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time/at/washington","tls":"","persist":false,"proxy":"","authType":"","x":790,"y":1680,"wires":[["b81d79e1.511c3"]]},{"id":"a6ab0c3d.70d12","type":"change","z":"78e6a193.5e7df8","name":"long, 12 text","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"long\",\"timeFormat\":\"12H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":1680,"wires":[["75f780d8.ada258"]]},{"id":"7d79eb2e.59e05c","type":"inject","z":"78e6a193.5e7df8","name":"OK request","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1680,"wires":[["a6ab0c3d.70d12"]]},{"id":"b81d79e1.511c3","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1680,"wires":[]},{"id":"b5275e02.a4328","type":"http request","z":"78e6a193.5e7df8","name":"GET Sydney","method":"GET","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time/at/sydney","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":1800,"wires":[["d3adc207.4e0168"]]},{"id":"cf2560d7.db8fc8","type":"change","z":"78e6a193.5e7df8","name":"short 24H, JSON","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"short\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":1800,"wires":[["b5275e02.a4328"]]},{"id":"1868ca63.89c47e","type":"inject","z":"78e6a193.5e7df8","name":"OK request","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1800,"wires":[["cf2560d7.db8fc8"]]},{"id":"d3adc207.4e0168","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1800,"wires":[]},{"id":"e88f7bee.e248e8","type":"http request","z":"78e6a193.5e7df8","name":"GET London","method":"GET","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time/at/london","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":1840,"wires":[["bc5cac20.bbf16"]]},{"id":"69c75c92.e67a0c","type":"change","z":"78e6a193.5e7df8","name":"long, 24H, JSON","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"long\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":1840,"wires":[["e88f7bee.e248e8"]]},{"id":"cf664a3a.dba968","type":"inject","z":"78e6a193.5e7df8","name":"OK request","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1840,"wires":[["69c75c92.e67a0c"]]},{"id":"bc5cac20.bbf16","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1840,"wires":[]},{"id":"575c33b5.0e6c7c","type":"http request","z":"78e6a193.5e7df8","name":"GET Singapore","method":"GET","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time/at/singapore","tls":"","persist":false,"proxy":"","authType":"","x":780,"y":1880,"wires":[["2b018ab3.2275b6"]]},{"id":"911e55a2.750718","type":"change","z":"78e6a193.5e7df8","name":"short, 12H, JSON","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"short\",\"timeFormat\":\"12H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":1880,"wires":[["575c33b5.0e6c7c"]]},{"id":"92859a3c.ef3eb8","type":"inject","z":"78e6a193.5e7df8","name":"OK request","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1880,"wires":[["911e55a2.750718"]]},{"id":"2b018ab3.2275b6","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1880,"wires":[]},{"id":"9f711c3b.27f078","type":"http request","z":"78e6a193.5e7df8","name":"GET London","method":"GET","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time/at/london","tls":"","persist":false,"proxy":"","authType":"","x":770,"y":1720,"wires":[["6018f59d.bdfdf4"]]},{"id":"97f3b14e.712c18","type":"change","z":"78e6a193.5e7df8","name":"short, 24, text","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"short\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":1720,"wires":[["9f711c3b.27f078"]]},{"id":"b0609d8f.9ff238","type":"inject","z":"78e6a193.5e7df8","name":"OK request","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1720,"wires":[["97f3b14e.712c18"]]},{"id":"6018f59d.bdfdf4","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1720,"wires":[]},{"id":"7207072e.1a1e38","type":"http request","z":"78e6a193.5e7df8","name":"GET Singapore","method":"GET","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time/at/singapore","tls":"","persist":false,"proxy":"","authType":"","x":780,"y":1760,"wires":[["5427b1d9.35b7"]]},{"id":"dfc04844.096218","type":"change","z":"78e6a193.5e7df8","name":"long, 24H, text","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"long\",\"timeFormat\":\"24H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"plain/text\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":1760,"wires":[["7207072e.1a1e38"]]},{"id":"96fe7b4c.18dc98","type":"inject","z":"78e6a193.5e7df8","name":"OK request","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1760,"wires":[["dfc04844.096218"]]},{"id":"5427b1d9.35b7","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1760,"wires":[]},{"id":"a4d72ff2.f67ca8","type":"http request","z":"78e6a193.5e7df8","name":"GET Washington","method":"GET","ret":"txt","paytoqs":true,"url":"http://localhost:1880/time/at/washington","tls":"","persist":false,"proxy":"","authType":"","x":790,"y":1920,"wires":[["e8194d54.647fa8"]]},{"id":"2c19725c.ddcdf6","type":"change","z":"78e6a193.5e7df8","name":"long, 12H, JSON","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"format\":\"long\",\"timeFormat\":\"12H\"}","tot":"json"},{"t":"set","p":"headers","pt":"msg","to":"{\"Accept\":\"application/json\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":1920,"wires":[["a4d72ff2.f67ca8"]]},{"id":"c58fa7e6.6b7df","type":"inject","z":"78e6a193.5e7df8","name":"OK request","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":1920,"wires":[["2c19725c.ddcdf6"]]},{"id":"e8194d54.647fa8","type":"debug","z":"78e6a193.5e7df8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":1920,"wires":[]},{"id":"d8d8db94.af8b88","type":"comment","z":"78e6a193.5e7df8","name":"time at GET API","info":"","x":200,"y":1200,"wires":[]},{"id":"a73242af.6499a","type":"comment","z":"78e6a193.5e7df8","name":"time POST API","info":"","x":200,"y":1420,"wires":[]}]

Part 2 - Local Node-RED - Web and REST APIs - Calling an API from Node-RED - Implementing an API in Node-RED - API Practical


Quick links : Home - Part 1 - Part 2 - Part 3 - Part 4 - Part 5