mani kandan
mani kandan

Reputation: 87

How to pass query parameters in rest api?

I have passed body parameters include method in single object to call api but iam unable to pass query parameters in the object.

code is

let fkPostPayload = {
        httpMethod: "POST",
        path: "/event",
        body: {
            "payload": {
                "application": {
                    "id": 11870117004,
                    "candidate": {
                        "id": 10921993004,
                        "first_name": "011200aaa0Test01FF",
                        "last_name": "01111200aa0Test01FF",
                        "title": null,
                        "is_private": true,
                        "can_email": true,
                        "external_id": null,
                        "phone_numbers": [],
                        "email_addresses": [
                            {
                                "value": "[email protected]",
                                "type": "personal"
                            }
                        ],
                    },
                }
            }
        },
        queryParam:{ "clientId":70, "countryCode":"BRA","partner":"AA-GMS" }
    }

i have passed the payload in api call, able to get body parameters value but unable to get queryparam values. suggest the better solution.

when i tried to get query params value using req.query its return empty object.

Upvotes: 1

Views: 1781

Answers (2)

mani kandan
mani kandan

Reputation: 87

otherwise simply we can pass using

queryStringParameters:{clientId:"70"}

Upvotes: 0

JanuszO
JanuszO

Reputation: 1240

Try to pass parameters in the path

import map from 'lodash/map'

const httpBuildQuery = function (url, queryParams) {
  const queryString = map(
    queryParams,
    function (val, key) {
      return encodeURIComponent(key) + '=' + encodeURIComponent(val)
    }
  ).join('&')

  if (queryString) {
    const separator = !url.includes('?')
      ? '?'
      : '&'

    return url + separator + queryString
  } else {
    return url
  }
}

let fkPostPayload = {
  httpMethod: "POST",
  path: httpBuildQuery("/event", { "clientId":70, "countryCode":"BRA","partner":"AA-GMS" }),
  body: {
      "payload": {
          "application": {
              "id": 11870117004,
              "candidate": {
                  "id": 10921993004,
                  "first_name": "011200aaa0Test01FF",
                  "last_name": "01111200aa0Test01FF",
                  "title": null,
                  "is_private": true,
                  "can_email": true,
                  "external_id": null,
                  "phone_numbers": [],
                  "email_addresses": [
                      {
                          "value": "[email protected]",
                          "type": "personal"
                      }
                  ],
              },
          }
      }
  }
}

Upvotes: 1

Related Questions