In this part, we will delve into concepts like `http` processor and `data
modelling` using a simple example.
We have created an array of records for group of countries. Each record has two
key attributes: **name** and **capital**, in the form of JSON data.
We will call this data through `http processor`, save the data as a `record` by
using `for loop` and display it in a tabular form.
By the end of this part, you will have gained insights into how using `fastn`,
REST APIs can seemlessly connect the backend with the frontend.
The first step is to create a `fastn package`.
ℹ️
What is `fastn` package?
`fastn` package is a folder that requires atleast two files
- FASTN.ftd
- index.ftd
http processor
`http` processor does the network communication using REST API and fetches data
from the external site in a different domain.
The syntax of using `http` processor is as follows:
-- import: fastn/processors as pr
-- record r:
$processor$: pr.http
Lang:
ftd
Data modelling: `record`
For this example, we will use `record` feature of `fastn`'s. `record` can be
used to create a user defined data type.
Here we are creating a new record called `country-data`.
Declaring the `country-record`
-- record country-data:
string name:
string capital:
Lang:
ftd
Start building
Let's implement the theory and build the project.
**Step 1:** Import `fastn/processors`
Copy the import line and paste it at the top of the `index.ftd` document
Import Processors
-- import: fastn/processors as pr
Lang:
ftd
**Step 2:** Declare a `record`
Before a record can be used it must be declared.
Declaring `country-data` record
-- record country-data:
string name:
string capital:
Lang:
ftd
**Step 3:** Create a list
Since the JSON data has a list of countries and their respective capitals,
therefore, we will create a list and use the `country-data` record as the type.
`countries` is a `list` of `country-data`
-- country-data list countries:
Lang:
ftd
`$processor$: pr.http` will initialise `countries` with the data returned
by the `url`.
-- country-data list countries:
$processor$: pr.http
url: https://famous-loincloth-ox.cyclic.app/
Lang:
ftd
**Step 4:** Data is ready, lets show it in the UI
Now that we have download the data from the `url` and stored it in `countries`,
we want to show it to user. To do that let's create a component called
`country-detail`.
Till now, we have just defined the component but to display the list of country
names we need call the component. Therefore, after closing the component we
can call the component and use a `for` loop.
-- country-detail: $country
for: $country in $countries
Lang:
ftd
Now wrap the two texts for country name and capital in `row` container
So you have successfully fetched and displayed the values of JSON data from the
external website using the `http` processor and one of the data modelling type,
`record`.
Improved UI
You can use various `fastn` properties to improve the UI to display the data,
let's say in the form of a table.