API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

Is it possible to retrieve the query parameter and pass to the body in the GET request

  • 1.  Is it possible to retrieve the query parameter and pass to the body in the GET request

    Posted Thu October 05, 2023 07:45 AM

    Hi
    Hope this finds you well.

    We have the following requirements for which expert opinion is required. 

    1. Retrieve the query parameter: We have achieved it through "$(request.parameters.state)" where the state is the name of one of the query parameters.

    2. We want to pass the value of the query parameter retrieved in step 1 as JSON object in body. or

    3. We want to convert the query parameter into json object 

    4. Access the json data from the object and evaluate the substring function as substring is JSONata expression and can be only applied to json data.This we have achieved through foloowing code 

    - extract:
                        version: 2.0.0
                        title: extract
                        root: message.body
                        extracts:
                          - capture: $.state
                            transform: $substring($,-4)
                            description: Include only the last 4 characters of the field.
    We will require the inputs for the step 2 or step 3 whichever is possible. 

    Note: We are using IBM API connect SaaS on AWS with v10 and the plan we subscribed is the base plan. Also we want to achieve it for the GET endpoint

    Do let us know if any other information is required from our end. 

    Thanks and Regards 
    Pragnesh Gandhi


    ------------------------------
    Pragnesh Gandhi
    ------------------------------


  • 2.  RE: Is it possible to retrieve the query parameter and pass to the body in the GET request

    Posted Thu October 05, 2023 05:34 PM

    Hi Pragnesh,
    For something simple, the set variable policy may work.  I'm assuming since you're saying AWS base plan you don't have the GatewayScript policy which would definitely be the preferred choice if it was available.

    For my quick test

    curl -k https://w.x.y.z:7444/swlinnorg/sandbox/test-query-to-json/path-1?state=1234567890
    {"state": "1234567890"}

    Was accomplished with this API assembly

      assembly:
        execute:
          - set-variable:
              version: 2.0.0
              title: set-variable
              actions:
                - set: message.body
                  value: '{"state": "$(request.parameters.state)"}'
                  type: any
                - set: message.headers.content-type
                  value: application/json
                  type: string

    Note if you want to subsequently do something with message.body.state, I believe message.body may be a string and if so, you'll need to parse it first.

    Obviously, the more complex the object you want to build the less readable the set-variable policy action value property will be.
    Best Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 3.  RE: Is it possible to retrieve the query parameter and pass to the body in the GET request

    Posted Wed October 25, 2023 04:27 AM

    Hi Steve
    Thanks for the input. As per your input we have tried the things but while accessing the message.body.state we are getting blank value. 

    -> please find below the steps we want to achieve and how we have written the same in YAML file.

    1. Retrieve the query parameter:

    -> We have achieved through the following assembly :

    execute:

    - set-variable:

    version: 2.0.0

    title: Account Information

    description: Account Information

    actions:

    - set: message.headers.state

    value: $(request.parameters.state)

    type: string

    2. We want to pass the value of the query parameter retrieved in step 1 as JSON object in body

    -> We tried this with the following assembly

    execute:

    - set-variable:

    version: 2.0.0

    title: Account Information

    description: Account Information

    actions:

    - set: message.body

    value: '{"state": "$(request.parameters.state)"}'

    type: any

    3. Access the json data from the object and evaluate the substring function as substring is JSONata expression and can be only applied to json data.This we have achieved through foloowing code

    - extract:

    version: 2.0.0

    title: extract

    root: message.body

    extracts:

    - capture: $.state

    transform: $substring($,-4)

    description: Include only the last 4 characters of the field.

    4. then we tried to access the json data filed and tried to set in the header

    - set-variable:

    version: 2.0.0

    title: Account Information

    description: Account Information

    actions:

    - set: message.headers.state

    value: $(message.body.state)

    type: string

    We are getting the value of message.headers.state as null or empty value.

    Do let us know if any other information is required from our end. Looking forward to hearing from you.

    Note: The above assembly we are trying for GET operation.

    Thanks and Regards

    Pragnesh Gandhi



    ------------------------------
    Pragnesh Gandhi
    ------------------------------