Go 将一个API请求的有效负载传递给API调用2
我喜欢创建两个API,其中请求在一个API中获取信息,并在另一个API调用中插入db。我怎样才能在光纤中实现这一点 考虑以下代码块Go 将一个API请求的有效负载传递给API调用2,go,go-fiber,Go,Go Fiber,我喜欢创建两个API,其中请求在一个API中获取信息,并在另一个API调用中插入db。我怎样才能在光纤中实现这一点 考虑以下代码块 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 t
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
}
如何使用POST-in-Go光纤框架调用第二个函数,以便将有效负载传递给另一个API。这是我的方法:
这是路由和处理程序的包
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
}