如何使用 ExpressJS 和 Axios 从 REST API 中提取某些字段名称

发布于 2025-01-11 06:43:33 字数 1889 浏览 0 评论 0原文

所以我有一组代码可以用这段代码拉取整个外部API:

//endpoint that will fetch data from an external API
app.get("/externalapi", (req, res) => {
    
    let apiURL = 'https://herokuapp.com/api/v1/data';
      
      axios.get(apiURL)
          .then(response => {
              
              res.status(200).json(response.data);
          })
          .catch((err) => {
              res.status(500).json({ message: err });
          });
  });

通过Postman生成的部分数据是这样的(用作示例):

{
    "data": [
        {
            "first_name": "Fiona",
            "last_name": "Smith",
            "phone_number": "987-3595-89",
            "mental_health_referral": false,
            "date_last_mental_health_referal": "02/09/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 6866318
        },
        {
            "first_name": "Richard",
            "last_name": "Stewart",
            "phone_number": "281-0394-41",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/23/2018T00:00:00.0000Z",
            "legal_councel_referal": false,
            "CHW_id": 9241074
        },
        {
            "first_name": "Andrew",
            "last_name": "Stevens",
            "phone_number": "068-8173-37",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/30/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 9241074
        }
}

现在我的目标是只生成Fiona的信息,这是第一个数据。我的 URL 概要是:

GET https://herokuapp.com/api/v1/data/{{first_name}}/{{last_name}}/{{phone_number}}

我尝试过类似的操作:

https://herokuapp.com/api/v1/data?first_name=Fiona&last_name=Smith&phone_number=987-3595-89

但我没有得到我想要的结果,当我在 Postman 上运行 get 请求时,我仍然得到 API 的所有结果。我缺少什么?

So I have a set of code that can pull the whole external API with this code:

//endpoint that will fetch data from an external API
app.get("/externalapi", (req, res) => {
    
    let apiURL = 'https://herokuapp.com/api/v1/data';
      
      axios.get(apiURL)
          .then(response => {
              
              res.status(200).json(response.data);
          })
          .catch((err) => {
              res.status(500).json({ message: err });
          });
  });

Part of the data that is generated via Postman is this (used as an example):

{
    "data": [
        {
            "first_name": "Fiona",
            "last_name": "Smith",
            "phone_number": "987-3595-89",
            "mental_health_referral": false,
            "date_last_mental_health_referal": "02/09/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 6866318
        },
        {
            "first_name": "Richard",
            "last_name": "Stewart",
            "phone_number": "281-0394-41",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/23/2018T00:00:00.0000Z",
            "legal_councel_referal": false,
            "CHW_id": 9241074
        },
        {
            "first_name": "Andrew",
            "last_name": "Stevens",
            "phone_number": "068-8173-37",
            "mental_health_referral": true,
            "date_last_mental_health_referal": "03/30/2018T00:00:00.0000Z",
            "legal_councel_referal": true,
            "CHW_id": 9241074
        }
}

Now my goal is to generate only Fiona's information, which is the first data. My outline for the URL is:

GET https://herokuapp.com/api/v1/data/{{first_name}}/{{last_name}}/{{phone_number}}

I have tried something like:

https://herokuapp.com/api/v1/data?first_name=Fiona&last_name=Smith&phone_number=987-3595-89

But I am not getting the result I want and when I run the get request on Postman, I still get all the results of the API. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2025-01-18 06:43:33

我认为你应该在 API 调用本身中传递参数,就像

 axios.get(apiURL, { params: { first_name: 'Fiona',last_name : 'Smith',phone_number: 987-3595-89 } }

I think you should pass the parameters in API call itself just like

 axios.get(apiURL, { params: { first_name: 'Fiona',last_name : 'Smith',phone_number: 987-3595-89 } }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文