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
GETrequest namedGet Pokemon. - Click the
Parametersbutton. - Click the
+button to add a new parameter. - Enter
pokemonNumberinKey. - Click
MOCKto navigate to the parameter definition in theInspector. - In
Component, selectdata2-core-primitives@Number. - In
Mock, enter1to simulate the parameter value.
Now we'll use this parameter in the URL:
- In the
Inspector, click< Inspectorto 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 Referencedefinition. - Enter
pokemonNumberinReference. - Click
Savein 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
JSONViewerto 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
JSONViewerto 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 Definitionscalledresponse. - In
State Definition, selectdata2-core-primitives@ObjectinComponentand inInitial Value, clickf()and selectObject. - In
On Click, clickf()and selectSeries. - In the first action of the
Series, inModule, select theHTTP Clientthat was created. - In
Interaction, selectsendRequest. - Click
Requestto enable the field, and select theList Pokemonsrequest (or whatever name was given). - Add a second action.
- Click
f()and select theuibutton next tosetter. This adds aSetterwithModulepre-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
Blockby clickingSaveor 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
JSONViewerparameters at the end of theInspector, changeJSONtoGetterby clickingf()and selectinguinext to thegetter(you could also click theGetterto navigate, and inModule, selectUIState). - In
Prop, clickPick Component, hover over the buttonBlock, and selectresponse. - Save the
JSONViewerby clickingSaveor deselecting. - Click the
Artboardpreview 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 DefinitionscalledpokemonNumber, selectdata2-core-primitives@NumberinComponent, and set theInitial Valueto 1. - Create another state in
State Definitionscalledresponse, selectdata2-core-primitives@ObjectinComponent, and inInitial Value, clickf()and selectObject. - In
On Click, clickf()and selectSeries. - In the first action of the
Series, inModule, select theHTTP Clientthat was created. - In
Interaction, selectsendRequest. - Click
Requestto enable the field, and select theGet Pokemonrequest (or whatever name was given). - Click
Request Parameters, and inpokemonNumber, clickf()and selectuinext togetter. Click to navigate. - In
Prop, clickPick Componentand select the buttonBlock. InState Prop, selectpokemonNumber. - Add a second action.
- Click
f()and select theuibutton next tosetter. This adds aSetterwithModulepre-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
Artboardpreview 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
POSTas 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, clickMockto 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
Inspectorroot and clickData. - In the header, next to
Object, click+to add a key to the object. - Enter
urlas the key and intype, selectdata2-core-types / Referenceto create the key with the correct type. - Click the added parameter, and in
Reference, enterurl. - Save.
- Click
Sendto 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).