How to Access URI and Query Parameter Inside Kumologica API
Understanding the usage of URI and query parameter is very important when developing an API. Read on to find out how!
Join the DZone community and get the full member experience.
Join For FreeAs an API developer, you would have designed and developed REST APIs that are intended to do some action over a particular resource, retrieval, or filtering of a collection of resources. These actions in the REST world are typically done with the help of a URI or query parameter. Understanding the usage of URI and query parameter is very important when developing an API.
URI Parameter
A URI is a resource identifier that can uniquely identify a specific instance of a resource. URI is part of the URL that is passed to get a unique resource.
eg: Get the details of a credit or debit card with unique id 738288383 and 489393 respectively.
http://abc.com/card/credit/738288383
http://abc.com/card/debit/489393
Query Parameter
Query parameters are used for filtering a resource collection. They are passed at the end of the URL after a question mark to sort, filter, or paginate the resource. In certain cases, they are also used along with the URI parameter.
eg: Get the credit cards that are in an active state and limit it by 100 cards.
http://abc.com/card/credit?status=active&limit=100
Now I think you might have got some idea around when to use URI and query parameter. In this article, I will demonstrate how to extract the URI and query parameter values inside the Kumologica API flow. For this, we will create two flows. The first flow will demonstrate how to access the URI parameter and the second flow will demonstrate how to access the query parameter.
Pre-Requisite
- Download and install the Kumologica designer.
- Install and configure AWS CLI
Note: For more details on installation refer to the following link.
Steps
Lets first see how to access the URI parameter inside an API flow developed using Kumologica.
- Drag and drop the Event Listener node from the palette and configure the following by opening the node settings.
Verb: GET
Path: /card/credit/:cardno
In the above configuration, Path is having:cardno as the notation to represent the URI parameter cardno.
2. Place the Logger node from the palette and provide the following as the Message. This will print the URI parameter in the AWS Cloudwatch log by default.
Message: msg.header.event.Records[0].pathParameters.cardno
In the above Kumologica expression, we are traversing the AWS lambda event payload.
3. Add EventListener End node to the canvas and set the Payload as given below. This will return the URI parameter as the plain text response
Content-Type: text/plain
Payload : msg.header.event.Records[0].pathParameters.cardno
4. Wire all the 3 nodes and will be getting the flow assembly as given below.
Now let's create the second flow to access the query parameter inside an API flow.
- Drag and drop the Event Listener node from the palette and configure the following by opening the node settings.
Verb: GET
Path: /card/credit
2. Place the Logger node from the palette and provide the following as the Message. This will print the query parameter in the AWS Cloudwatch log by default.
Message: msg.header.event.Records[0].queryStringParameters.status
In the above Kumologica expression, we are traversing the AWS lambda event payload to fetch the query parameter.
3. Add EventListener End node to the canvas and set the Payload as given below. This will return the query parameter as the plain text response
Content-Type: text/plain
Payload: msg.header.event.Records[0].queryStringParameters.limit
4. Wire all the 3 nodes and will be getting the flow assembly as given below.
Deploy and Test
- Go to the cloud tab and select the AWS profile.
- Go to the trigger section and selection API Gateway as the trigger and set the following.
Deployment stage: test
3. Click Deploy. Once deployed copy the URL for testing.
4. Go to POSTMAN or any rest client of your choice and provide the below.
GET https://fjdj383ij.amazonaws.com/test/card/credit/28892933
You should get the response as 28892933.
5. Go back to POSTMAN and provide the below.
GET https://fjdj383ij.amazonaws.com/test/card/credit/?status=active&limit=100
You should get the response as 100.
In the AWS CloudWatch logs, you can also see the logger printing the card number (first flow) and status (second flow).
Summary
I hope you have enjoyed the article and able to understand the concept of URI and query parameter and the way to access both the values inside Kumologica flow. Please share your valuable feedback and queries on the comments box and see you in my next article. Thank you.
Opinions expressed by DZone contributors are their own.
Comments