Skip to content

Instantly share code, notes, and snippets.

@AnjaneyuluBatta505
Forked from konojunya/main.go
Created April 1, 2020 05:24
Show Gist options
  • Save AnjaneyuluBatta505/2f1afeaf6353841388f48aa31fc0190d to your computer and use it in GitHub Desktop.
Save AnjaneyuluBatta505/2f1afeaf6353841388f48aa31fc0190d to your computer and use it in GitHub Desktop.
Sample using gin's BindJSON
package main
import (
"fmt"
"log"
"github.com/gin-gonic/gin"
)
type CreateParams struct {
Username string `json:"username"`
Guests []Person `json:"guests"`
RoomType string `json:"roomType"`
CheckinDate string `json:"checkinDate"`
CheckoutDate string `json:"checkoutDate"`
}
type Person struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
func main() {
r := gin.Default()
r.POST("/", func(c *gin.Context) {
var createParams CreateParams
err := c.BindJSON(&createParams)
if err != nil {
log.Fatal(err)
}
fmt.Println(createParams)
})
r.Run(":8000")
// request
// curl http://localhost:8000 -d @request.json
}
{
"guests": [
{
"firstname": "ryan",
"lastname": "vlaming"
}
],
"roomType": "suite",
"checkinDate": "2018-04-13T19:24:00+00:00",
"checkoutDate": "2018-07-13T19:24:00+00:00"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment