HTTP Client Requests
In some applications, integrating with external services to fetch or send data is necessary. This is done through HTTP requests to these services' APIs. We can perform these requests using Data² Connect's HTTP Client
.
This guide aims to show some basic features of the Data² Connect HTTP Client
and how to use it in your applications.
We will use PokeAPI
to make GET
requests to get a list of Pokémon or information about a specific Pokémon, and CleanURI
to shorten URLs via POST
requests.
The final result is available at the end of this guide, ready to be copied and pasted into your Builder.
Creating an HTTP Client
To create an HTTP Client
, double-click on the Builder background, select Connect
, and then click HTTP Client
.
Your first request
To create a new request, click the +
button in the lower-left corner. This adds a new request to the list. We'll use PokeAPI
as an example.
Select GET
next to New HttpRequest
, and rename your request to List Pokemons
. In the URL field, enter https://pokeapi.co/api/v2/pokemon/
(endpoint documentation) to return a list of Pokémon.
Click the SEND
button to test the request and view the response in the panel. You can explore the response in Text
, HTML
, or JSON
formats.
Using parameters
According to the PokeAPI documentation, you can get detailed information about a Pokémon using its identification number.
We could put the number directly in the URL, but it's better to make this a configurable URL parameter so we can reuse the same request to fetch a different Pokémon.
We'll add a parameter to our request. For this step, leave the Inspector
open.
- Create a new
GET
request namedGet Pokemon
. - Click the
Parameters
button. - Click the
+
button to add a new parameter. - Enter
pokemonNumber
inKey
. - Click
MOCK
to navigate to the parameter definition in theInspector
. - In
Component
, selectdata2-core-primitives@Number
. - In
Mock
, enter1
to simulate the parameter value.
Now we'll use this parameter in the URL:
- In the
Inspector
, click< Inspector
to navigate to the request definition root. - In
URL
, click thef()
and selectStringConcat
. - Click
+
to add a new item, and enterhttps://pokeapi.co/api/v2/pokemon/
. - Add another item, click
f()
, selectContext Reference
. - Click to navigate to the
Context Reference
definition. - Enter
pokemonNumber
inReference
. - Click
Save
in theInspector
.
Test your request by clicking Send
in your HTTP Client
. If correct, you will see information about Pokémon number 1 (Bulbasaur).
Using the HTTP Client in an application
To use the HTTP Client
in an Artboard
, we'll create:
- A button that makes the Pokémon list request and saves the result in its UI State.
- A
JSONViewer
to view the request result. - A text field for the user to choose the Pokémon number they want to fetch.
- A button that makes the chosen Pokémon request and saves the result in its UI State.
- Another
JSONViewer
to view the request results.
Creating the interface
List Pokémon
First, create an Artboard
. Then, draw a Block
for the button and another Block
for the JSONViewer
.
In the block that will serve as the button, we’ll define the state (response) and the action to send the request, retrieve the result, and save it in the state:
- In
Text
, writeList Pokémons
. - Create a new state in
State Definitions
calledresponse
. - In
State Definition
, selectdata2-core-primitives@Object
inComponent
and inInitial Value
, clickf()
and selectObject
. - In
On Click
, clickf()
and selectSeries
. - In the first action of the
Series
, inModule
, select theHTTP Client
that was created. - In
Interaction
, selectsendRequest
. - Click
Request
to enable the field, and select theList Pokemons
request (or whatever name was given). - Add a second action.
- Click
f()
and select theui
button next tosetter
. This adds aSetter
withModule
pre-filled forUIState
. - Click the
Setter
, thenPick Component
. Click the buttonBlock
, and inState Prop
, selectresponse
. - In
Value
, clickf()
and selectContext Reference
. - In
Reference
, writeprevResult
. - Style the button as desired.
- Save the
Block
by clickingSave
or deselecting theBlock
.
In the JSONViewer
block, display the request result by reading from the button’s UIState
:
- Change its type to
data2-blocks@JSONViewer
. - In the
JSONViewer
parameters at the end of theInspector
, changeJSON
toGetter
by clickingf()
and selectingui
next to thegetter
(you could also click theGetter
to navigate, and inModule
, selectUIState
). - In
Prop
, clickPick Component
, hover over the buttonBlock
, and selectresponse
. - Save the
JSONViewer
by clickingSave
or deselecting. - Click the
Artboard
preview button to test the button.
Pokémon Information
In the same Artboard
, draw a Block
for the text field, a Block
for the button, and another Block
for the JSONViewer
.
In the button block, define the states (response
and pokemonNumber
), and the on click action of sending the request, and saving the result in the UIState
:
- In
Text
, writeGet Pokémon
. - Create a new state in
State Definitions
calledpokemonNumber
, selectdata2-core-primitives@Number
inComponent
, and set theInitial Value
to 1. - Create another state in
State Definitions
calledresponse
, selectdata2-core-primitives@Object
inComponent
, and inInitial Value
, clickf()
and selectObject
. - In
On Click
, clickf()
and selectSeries
. - In the first action of the
Series
, inModule
, select theHTTP Client
that was created. - In
Interaction
, selectsendRequest
. - Click
Request
to enable the field, and select theGet Pokemon
request (or whatever name was given). - Click
Request Parameters
, and inpokemonNumber
, clickf()
and selectui
next togetter
. Click to navigate. - In
Prop
, clickPick Component
and select the buttonBlock
. InState Prop
, selectpokemonNumber
. - Add a second action.
- Click
f()
and select theui
button next tosetter
. This adds aSetter
withModule
pre-filled forUIState
. - Click the
Setter
, thenPick Component
. Click the buttonBlock
, and inState Prop
, selectresponse
. - In
Value
, clickf()
and selectContext Reference
. - In
Reference
, writeprevResult
. - Style the button, and save.
In the JSONViewer
block, view the request result by reading from the button's UIState
:
- Change its type to
data2-blocks@JSONViewer
. - Click
JSON
, and inModule
, selectUIState
. - In
Prop
, clickPick Component
, hover over the buttonBlock
, and selectresponse
. - Save.
In the text field block, edit the UIState
pokemonNumber
in the button, so the user can type the Pokémon number they want to fetch:
- Change the type to
data2-blocks@DataFieldBlock
. - In
Dataset
, selectUIState
. - In
Field
, clickPick Component
, hover over the buttonBlock
, and selectpokemonNumber
. - In
Type
, selectnumber
. - Save.
- Click the
Artboard
preview button to test the button.
Example: POST request to shorten URLs
We will create a POST request to https://cleanuri.com/api/v1/shorten
, an API that shortens URLs (cleanuri docs). It expects a JSON with the key url
and the value of the URL we want to shorten. Example:
{
"url": "https://google.com/"
}
- Click
+
to add a new request. - Select
POST
as the method and rename the request toShorten URL
. - In the URL field, enter
https://cleanuri.com/api/v1/shorten
. - In
Parameters
, add a parameter namedurl
, clickMock
to navigate to the parameter definition in theInspector
. - In
Component
, selectdata2-core-primitives@String
. - In
Mock
, enterhttps://google.com/
to simulate the parameter value. - Navigate to the
Inspector
root and clickData
. - In the header, next to
Object
, click+
to add a key to the object. - Enter
url
as the key and intype
, selectdata2-core-types / Reference
to create the key with the correct type. - Click the added parameter, and in
Reference
, enterurl
. - Save.
- Click
Send
to test the request.
Conclusion
In this article, we learned how to create an HTTP Client
and make HTTP requests. We saw how to add parameters to requests and how to use the HTTP Client
in an application.
As a challenge, try making a POST request to the URL shortener, where the user can enter the URL they want to shorten.
Final result
The final result is available below (and can be copied and pasted into your Builder
).