0

I like to create two API in which request is made to get information in one API and the insertion to the db is made in another API call. How could I able to achieve this in Fiber.

consider the following code block

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
}

How to call the second function with POST in Go fiber framework, so that I pass the payload to another API.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Balaji.J.B
  • 618
  • 7
  • 14

1 Answers1

2

This is my approach:

Here is package for routing and handler

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
   })
})

Here is package for saving and reading from database

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
}
Fahim Bagar
  • 798
  • 7
  • 17