Query and path parameters in IBM API Connect
Here in this blog, we are going to learn about Query and path parameters in IBM API Connect and also how to develop an API with a Query and path parameter in API Connect.
Query Parameter:
API Query parameters can be defined as the optional key-value pairs that appear after the question mark in the URL. Basically, they are extensions of the URL that are utilized to help determine specific content or action based on the data being delivered. Query parameters are appended to the end of the URL, using a ‘?’. The question mark sign is used to separate path and query parameters.
If you want to add multiple query parameters, an ‘&’ sign is placed in between them to form what is known as a query string. It can feature various object types with distinct lengths such as arrays, strings, and numbers.
Example:
- https://example.com/articles?sort=ASC&page=2
In this URL, there are two query parameters, sort, and page, with ASC and 2 being their values, respectively.
Path Parameter:
Path parameters are request parameters attached to a URL that points to a specific REST API resource. The path parameter is separated from the URL by a `/`. The path parameter defines the resource location. Each path parameter must be substituted with an actual value when the client makes an API call. The path parameter is contained within curly braces.
Example:
`/Employee/{id}`
`{id}` is the path parameter of the endpoint
`/Employee`- it is pointing to a specific employee’s record
The `/Employee/{id}` path parameter takes a geographic beach code to look up the resource associated with that code.
Query vs Path Parameters:
The first difference between query and path parameters is their position in the URL. While the query parameters appear on the right side of the ‘?’ in the URL, path parameters come before the question mark sign.
Secondly, the query parameters are used to sort/filter resources. On the other hand, path parameters are used to identify a specific resource or resources.
Let’s develop an API with a Query parameter in API Connect.
- Open the API Manager.
- Create an API and configure it as follow
3. Give the path.
4. Now go to operations in that select the get method.
5. In the get method give the parameters as follow.
6. Now go to the Gateway and create a flow as follow.
Invoke>>need to attach request query parameters to backend URL: http://demo5491410.mockable.io/thisisanusha?id=789&name=anusha
7. Now save the API.
8. And publish the API to a particular catalog.
9. Test the API.
Response:
Let’s develop an API with a Path parameter in API Connect.
- Open the API Manager.
- Create an API and configure it as follow
3. Give the path as {pathparam}.
4. Now go to operations in that select the get method.
5. In the get method give the parameters as follow.
6. Now go to the Gateway and create a flow as follow.
Invoke>>need to pass the request path parameter value to the backend URL: http://demo5491410.mockable.io/{ad}
7. Now save the API.
8. And publish the API to a particular catalog.
9. Test the API.
Response: