我喜欢创建两个API,其中请求在一个API中获取信息,并在另一个API调用中插入到数据库中。我怎样才能在光纤中做到这一点。
考虑以下代码块
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纤程框架中使用POST调用第二个函数,以便我将有效负载传递给另一个API。
发布于 2020-08-01 22:16:33
这是我的方法:
这是用于路由和处理程序的包
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
}https://stackoverflow.com/questions/63205273
复制相似问题