【问题标题】:Passing the payload of one API request to API call 2将一个 API 请求的负载传递给 API 调用 2
【发布时间】:2020-11-22 02:15:00
【问题描述】:

我喜欢创建两个 API,其中请求在一个 API 中获取信息,并在另一个 API 调用中插入到数据库。我怎么能在 Fiber 中实现这一点。

考虑下面的代码块

func getName(c *fiber.Ctx) {
   // get the name api
   // call the insertName func from here with name argument
   insertName(arg)
}

func insertName() {
   // insert the argument to the database
}

如何在 Go Fiber 框架中使用 POST 调用第二个函数,以便我将有效负载传递给另一个 API。

【问题讨论】:

    标签: go go-fiber


    【解决方案1】:

    这是我的方法:

    这里是路由和处理程序包

    package path
    
    // ./path/name
    app.Get("/name", func(c *fiber.Ctx) {
       p := controller.Name{name: "random_name"}
    
       result := controller.InsertName()
       c.JSON(fiber.Map{
          "success": result
       })
    })
    
    app.Post("/name", func(c *fiber.Ctx) {
       p := new(controller.Name)
    ​
       if err := c.BodyParser(p); err != nil {
          log.Fatal(err)
       }
    
       result := controller.InsertName(p)
       c.JSON(fiber.Map{
          "success": result
       })
    })
    

    这里是保存和读取数据库的包

    package controller
    
    // ./controller/name
    type Name struct {
        Name string `json:"name" xml:"name" form:"name"`
    }
    
    func insertName(n Name) bool {
       // insert the argument to the database
       return resultFromDatabase
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 2021-07-15
      • 2019-05-19
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多