Making HTTP Requests Through REST API Browser (Beta)¶
In this document, we will see how we can make different HTTP requests through REST API Browser (Beta) in Astera Centerprise.
Making a GET Request¶
To make a GET request, drag and drop the relevant GET request object from the REST API Browser (Beta) onto the designer window and pass your required PetId into it.
In this scenario, we want to get the info for pet with the PetId 3. For that, we will select the GET request ‘Find Pet by ID’ and pass the relevant PetId using a ConstantValue object.
To explore the REST Client object for this method, right-click on its header and select Properties.
This will open the REST API Client (Beta) screen where the connection info of your API is defined.
Shared REST API Connection– shows the name of your REST API Connection. You can select any shared REST API Connection you want to use from the drop-down list. The connections for all the APIs that have been imported in the project will be shown here
HTTP METHOD – shows the HTTP method of your call
Resource – shows what information you want to get from a specific API. It is a part of the Url of request sent
Content Type – shows the type of your content
All the above information is configured according to the request method that you have selected.
Click Next to proceed to the Parameters screen.
Here, you will see the following options:
Name: It contains the name of the parameter.
Rest Parameter Location: It tells which parameter is being used to make the call. There are three parameters which Centerprise supports:
- URL: This parameter is necessary to be specified.
- Query: This parameter may or may not be optional and can work as a filter while processing records.
- Header: This parameter is generally used for authentication purpose.
Click Next to proceed to the REST API Client Output Layout (Beta) screen where you can build the Response Layout of your API. There are two ways in which you can generate the output layout:
- The first one is by providing sample text by clicking on ‘Generate Layout by providing Sample Text’ option.
- The other way to do that is by running a request by clicking the ‘Generate Layout by running Request’ option.
Click Next to proceed to the Rest API Pagination Options (Beta).
Pagination refers to managing the traffic of records coming from a source. It divides the records into discrete number of pages so that they are comprehensible for a user. Not every API supports pagination. For those that do, Astera Centerprise offers three types of pagination.
It requires two parameters -a limit and an offset value to be specified by the user. A limit specifies the number of records which you want to specify in one page and an offset simply tells the number of records to be skipped before selecting records.
- Initial Offset – the record from which you want to start your pagination is specified here.
- Number of Pages – the number of pages which you want to be processed are specified here.
This type of pagination generates a token for the next page of records. You can set a limit to the number of pages you want to process.
- Field Location - there are two options where you can point your cursor: body, or header.
- Cursor Field – here, the field you want to attach your cursor to is specified.
- No of Pages – here, the number of pages you want to see in your output are specified or you can simply check the option of ‘Read Till End’ if you want to process all the records without any limit.
It is the same as Cursor pagination, except that it generates a URL for every next page instead of a token.
Click Next to proceed to the the Service Options (Beta) screen.
Here you can see the following service options:
Request Delay – the time (in milliseconds) to be delayed before sending a request
Retry Count – the number of retries to be made in case of a failed request
Retry Delay – the time (in milliseconds) to be delayed before making a request again
Ignore HTTP Status Codes checkbox is used to skip the error response for unsuccessful requests
Use Parallelism - this option specifies if you want multiple requests to be run in parallel and the number of those requests can be specified in Degree of Parallelism option
Once you have specified these options, click OK.
Making a POST Request¶
Now, let’s try posting a pet to the petstore. For that, we will drag the ‘Add a new pet to the store’ request from the REST API Browser (Beta) panel and drop it on to the dataflow designer.
We will pass the required parameters to the POST request object using a Variables object.
Now, right-click on the REST API Client (addPet) object and select Preview Output.
You can see that the ‘HTTPStatusCode’ is 200 which means that the REST API has successfully carried out whatever action the client requested. Let’s verify it by making a GET request for the same PetId which we posted.
You can see that a GET request for PetId 1 has returned us the same information which we had posted.
Making a DELETE Request¶
Now, Let’s try making a DELETE request. For that, we will first make a GET request to see if that pet exists on the petstore or not and try to delete that. We will pass PetId 5 using a ConstantValue object.
Right-click on the REST API Client (getPaidById) object and select Preview Output.
It has given the details of the pet with PetId 5, and the status shows that the pet is available on the pet store.
To delete this pet record, we will drag and drop the ‘Deletes a pet’ request from the REST API Browser (Beta) panel on to dataflow designer.
Pass PetId 5 to the DELETE request object using a ConstantValue object.
Right-click on the REST API Client object and select Preview Output. You can see that it has returned ‘HTTPStatusCode’ 200 which means a successful execution.
Let’s verify it by making a GET request again and check if pet with PetId 5 has been deleted or not.
Right-click on the REST API Client (getPetById) object and select Preview Output.
You can see that Centerprise has returned error 404 which means that there is no pet found with PetId 5 and the pet record has been successfully deleted from petstore API.
Working with APIs That Require Authentication¶
Let’s try making an API request that needs authentication. We’ll be making a GET request object using Adafruit’s API.
First, import the API by using the relevant import source. In this case, we will import using the Json/Yml Url import source.
You will see that all the APIs present on Adafruit’s URL have been populated in the REST API Browser (Beta).
Now you need to authenticate Adafruit’s APIs to use them in your dataflow. Without authentication, you will get an error. To authenticate an API, go to the Project Explorer and double click on the API’s .sact file under the Shared Connection node. In this case, we will double-click on Adafruit’s .sact file.
Adafruit’s .sact file will open on the designer. Now, right click on shared action file’s header and select Properties.
This will open the REST API Connection window, where you can configure the settings to authenticate Adafruit’s API.
Select the Security Type and specify the relevant details.
You can see that the Security Type has been specified as API Key which means that the Adafruit’s API uses ‘API Key’ as its authentication method.
To see the authentication methods supported in Centerprise, click on the Security Type drop-down list.
You will see the following options:
- O Auth 2
- API Key
- Basic Authentication
- Bearer Token
- No Authentication
Click OK. Now that the API has been authenticated, we can work with the API and make HTTP requests.
To make a GET request, drag and drop the ‘Get feed by key’ object from the REST API Browser (Beta) on to the dataflow designer.
It requires two parameters: username and feed_key. We will pass the values to these parameters either by using a source object via mapping it or by specifying the parameter value in the default value column in the properties of ‘getFeed’ object.
In this case, we have provided the username through a constant value object. Now let’s specify the feed_key in the Default Value column in the properties of the getFeed object.
Right click on the REST API Client (getFeed) and select Properties. Go to the Parameters screen and specify the feed_key value in the Default Value column.
Click Next to proceed to the REST API Client Output Layout (Beta) screen. Here, you can see the Response Layout of your selected method.
Click Next to proceed to the the Service Options screen.
Now, right click on the REST API Client (getFeed) object and select Preview Output.
You can see that Centerprise has returned the feed information by providing the required parameters.
Centerprise’s Server API¶
Now, let’s understand how to authenticate Centerprise’s Server API. The first step is to import a Custom API and provide the required credentials.
A node will be added in the REST API Browser (Beta) panel.
Now you can add HTTP methods for server operations by adding requests.
(To learn how to add a request in a Custom API, click here)
Although we have successfully added a node for our Custom API, we are yet to authenticate it. Centerprise’s server API uses ‘Bearer Token’ authentication.
To authenticate your custom API, go to the .Sact file in the Shared Connections.
A new tab by the name of ServerInfo.Sact will open. Here, you can see an object where you can configure the authentication settings for the API.
Right-click on the header of ServerInfo object and go to Properties.
This will open the REST API Connection (Beta) screen. Select the Security Type as ‘Bearer Token.’
Provide the User Name, Password, and Token URL for Bearer Token and click OK.
Now, that we have authenticated our custom API, we can make the required HTTP requests.
Drag and drop the Jobs object from the REST API Browser (Beta) panel.
Right-click on Jobs object header and select Preview Output.
This is how your output would look like:
This concludes making HTTP requests through REST API Browser (Beta) in Astera Centerprise.