Skip to content

How to: post data to your dashboard and widgets

Bruno P. Kinoshita edited this page Jun 26, 2020 · 4 revisions

When you install Smashing, it will be listening for HTTP POST requests that can be used to update the dashboard and widgets.

Authentication

Look at config.ru for a line that looks like set :auth_token, 'YOUR_AUTH_TOKEN'. You should change that value to a custom token value to be used by clients securely.

Message format

Let's use the default dashboard and widgets that come installed with Smashing by default.

Widgets

You can update any of the values with an HTTP POST request similar to the following.

curl -X POST -H 'Content-Type: application/json' -d '{ "auth_token": "YOUR_AUTH_TOKEN", "value": 9999 }' http://localhost:3030/widgets/synergy

Now breaking down the command.

  1. curl is the command used, you can use any other command line utility, or a GUI, or code in Shell, Python, etc
  2. -X POST defines this is an HTTP POST request
  3. H 'Content-Type: application/json sets the HTTP request content type header
  4. -d '{ "auth_token": "YOUR_AUTH_TOKEN", "value": 9999 }' specifies the data of the request, which is a JSON document. The auth_token, as mentioned above, is the default value which you probably want to alter. The "value" matches what is sent in the jobs/sample.rb job.
  5. http://localhost:3030/widgets/synergy is the URL used in the post, which contains the prefix http://localhost:3030/widgets/ followed by the widget name, which also matches the value from jobs/sample.rb job.

You should be able to craft the message format according to your widget data. You can look at the job Ruby file, and find the send_event call. The format of the data for a widget will be detailed there.

Dashboards

Besides sending data to widgets, you can also post to a dashboard with the URL http://localhost:3030/dashboards/:id where :id must match your dashboard name.

curl -X POST -H 'Content-Type: application/json' -d '{ "auth_token": "YOUR_AUTH_TOKEN", "event": "reload" }' http://localhost:3030/dashboards/sample

Now breaking down the command.

  1. curl is the command used, you can use any other command line utility, or a GUI, or code in Shell, Python, etc
  2. -X POST defines this is an HTTP POST request
  3. H 'Content-Type: application/json sets the HTTP request content type header
  4. -d '{ "auth_token": "YOUR_AUTH_TOKEN", "event": "reload" }' specifies the data of the request, which is a JSON document. The auth_token, as mentioned above, is the default value which you probably want to alter. The "event" is handled by the dashboard and forces a reload of the page.
  5. http://localhost:3030/dashboards/sample is the URL used in the post, which contains the prefix http://localhost:3030/dashboards/ followed by the dashboard name, which also matches the value from the dashboard URL.

The reload event is handled the CoffeeScript code that is serving the dashboard. You can have a look at the source code and add new events and their handlers.

Source code

If you would like to look at the code handling the requests, look at this file and search for post '/.

You should notice that in the end it uses send_event just like Smashing jobs.

Clone this wiki locally