Rest Query

<script type="edgeside/rest" ...>

Fetches data from any service that exposes a REST API.

Current limitations:

Attributes

Name Description Required?
data-edgeside-output specifies a key under which it should output data yes
data-edgeside-endpoint specifies the endpoint for the REST query yes
data-edgeside-cache-ttl specifies a Time To Live value for caching of query results in the Cloudflare cache, in seconds (default = 60) no
data-edgeside-parameter-map provides an optional mapping between request params or URL segments in the original page request and parameters to the query. (see Parameter Mapping) no
data-edgeside-input specifies an optional key under which it should be able to find input data which can be used in a parameter map no

Element content

Empty.


Examples

example1 - A Simple REST Query

We execute a simple REST query, in this case to a service which provides time data. We dump the output to the screen using the Debug element.

<script type="edgeside/rest"
data-edgeside-output="londonTime"
data-edgeside-endpoint="http://worldtimeapi.org/api/timezone/Europe/London">

</script>
<script type="edgeside/debug"
data-edgeside-input="londonTime">

</script>

Live Results:

Live Results (Source):


example2 - A Parameterised REST Query (From Request Params)

We execute a REST query that takes parameters, in this case to a service which provides time data for a time zone. Our query requests data about a time zone, with the country region and place taken from request parameters. We dump the output to the screen using the Debug element.

<script type="edgeside/rest"
data-edgeside-output="time"
data-edgeside-endpoint="http://worldtimeapi.org/api/timezone/$region/$place"
data-edgeside-parameter-map="$region:region,$place:place">

</script>
<script type="edgeside/debug"
data-edgeside-input="time">

</script>

Live Results:

Live Results (Source):


example3 - A Parameterised REST Query (From URL Segments)

We execute a REST query that takes parameters, in this case to a service which provides time data for a time zone. Our query requests data about a time zone, with the country region and place taken from URL segments. We dump the output to the screen using the Debug element. Note that in order to support responding to multiple URLs from a single HTML page, we will also need to use URL Rewriting.

<script type="edgeside/rest"
data-edgeside-output="time"
data-edgeside-endpoint="http://worldtimeapi.org/api/timezone/$region/$place"
data-edgeside-parameter-map="$region:/2,$place:/1">

</script>
<script type="edgeside/debug"
data-edgeside-input="time">

</script>

Live Results:

Live Results (Source):


example4 - A REST Query that uses request params (From Request Params)

We execute a REST query that takes parameters as request params on the query endpoint, in this case to a service which provides recipe searches. We map 'keywords' and 'ingrediants' request params from the page to the 'i' and 'q' params required by the service. We output the result using the Template element.

<script type="edgeside/rest"
data-edgeside-output="recipeData"
data-edgeside-endpoint="http://www.recipepuppy.com/api/"
data-edgeside-parameter-map="i:ingredients,q:keyword">

</script>
<script type="edgeside/template"
data-edgeside-input="recipeData">

<ul>
{{#results}}
<li><a href="{{href}}">{{title}}</a></li>
{{/results}}
</ul>
</script>

Live Results:

Live Results (Source):