簡體   English   中英

如何在rest api中傳遞查詢參數?

[英]How to pass query parameters in rest api?

我已經在單個對象中傳遞了 body 參數 include 方法來調用 api 但我無法在對象中傳遞查詢參數。

代碼是

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": "02000111aaa111Test01FF@test.com",
                                "type": "personal"
                            }
                        ],
                    },
                }
            }
        },
        queryParam:{ "clientId":70, "countryCode":"BRA","partner":"AA-GMS" }
    }

我已經在 api 調用中傳遞了有效負載,能夠獲取主體參數值但無法獲取 queryparam 值。 建議更好的解決方案。

當我嘗試使用 req.query 獲取查詢參數值時,其返回空對象。

嘗試在路徑中傳遞參數

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": "02000111aaa111Test01FF@test.com",
                          "type": "personal"
                      }
                  ],
              },
          }
      }
  }
}

否則我們可以通過使用

queryStringParameters:{clientId:"70"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM