To Filter Users By ID Using a Parameter

This procedure shows how to set up query parameters to filter data consumed by the API. Showing query parameters was chosen for example purposes instead of the conventional approach of using a path segment to get the unique identifier of a user. In the procedure, you add logic to the API to return information about user having ID = n.

Revise the flow to filter users by ID:

  1. Drag an HTTP Listener component from the Mule palette to the Source section of the get:/users/userbyid:api-config flow.

  2. In the Listener properties editor, in Connector Configuration of the HTTP Listener, select api-httpListenerConfig.

    Click Edit-16x16, and then OK to accept the default host and port 8081.

  3. In the Listener properties editor, in General, set Path to /placeholder/byid.

  4. Remove Transform Message from the Process area.

  5. Drag an HTTP Request component to the Process area to the left of Transform Message.

  6. In Configuration, select HTTP_Request_configuration. Click Edit-16x16, and accept the default settings:

    • Host = jsonplaceholder.typicode.com

    • Port = 80.

      Click OK.

  7. In the properties editor, in General, set these HTTP Request options:

    • Path: Set to /users.

    • Method: Accept the default method GET.

    • Select the Query Parameters tab, click Add-16x16, and replace "key" and "value" with the following name and value (no quotation marks):

      • Name: id

      • Value to attributes.queryParams.id

      • Accept the other defaults.

        The general option in the request tab is displayed to show the name ID, the value attributes.queryParams.id, and the other defaults.
  8. In the properties editor, set metadata properties as follows:

    • Select Metadata.

      Metadata option is highlighted in the Request tab.
    • Declare the data type of the id as follows:

      • Select Input: Payload from the drop-down.

      • Name the type using an arbitrary name, for example, int-type.

      • Define the type as an integer.

        Select metadata type window is displayed with the previously mentioned options.
  9. Save all.

Next, add a service to get only user names

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub