15

I'm starting to develop a REST API using Go and package Gin-Gonic. The idea is to create a REST API that receives POST requests in a JSON format and redirects this call to another application (also a API). Here is a piece of code:

package main

import (
    "fmt"

    "github.com/gin-gonic/gin"
    "net/http"
)
func main() {
    r := gin.Default()
    r.GET("/status", func(c *gin.Context) {
        c.String(200, "on")
    })

    r.GET("/user/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.String(http.StatusOK, "Hello %s", name)
    })

    r.GET("/user/:name/:action", func(c *gin.Context) {
        name := c.Param("name")
        action := c.Param("action")
        message := name + " is " + action
        c.String(http.StatusOK, message)
    })

    r.POST("/foo", func(c *gin.Context) {
        fmt.Printf("%s", "At least I got here")
        message := c.PostForm() //???
        c.JSON(200, gin.H{"status": message}) //???
    })
    r.Run(":8080") // listen an
}

At function r.Posts("/foo",...) I would like c.JSON to send me back the full JSON that I sent:

curl -H "Content-Type: application/json" -X POST -d '{"user":"xyz","password":"xyz"}' http://localhost:8080/foo

I've seen examples where they bind the JSON file by creating a struct with the same structure as the input JSON (check Gin-Gonic examples at https://github.com/gin-gonic/gin ). However I only need to resend the full string without taking care of the format. Any ideas?

Mark Lakata
  • 19,989
  • 5
  • 106
  • 123
Salami
  • 317
  • 1
  • 2
  • 9
  • I ended up creating a struct and parsing my json to the struct. As far as I know I can't simply get any JSON. – Salami Apr 12 '17 at 12:38

2 Answers2

20

Try this example:

package main

import (
    "fmt"

    "github.com/gin-gonic/gin"
    "net/http"
)

type LOGIN struct{
    USER string `json:"user" binding:"required"`
    PASSWORD string `json:"password" binding:"required"`
}

func main() {
    r := gin.Default()
    r.GET("/status", func(c *gin.Context) {
        c.String(200, "on")
    })

    r.GET("/user/:name", func(c *gin.Context) {
        name := c.Param("name")
        c.String(http.StatusOK, "Hello %s", name)
    })

    r.GET("/user/:name/:action", func(c *gin.Context) {
        name := c.Param("name")
        action := c.Param("action")
        message := name + " is " + action
        c.String(http.StatusOK, message)
    })

    r.POST("/foo", func(c *gin.Context) {
        var login LOGIN
        c.BindJSON(&login)
        c.JSON(200, gin.H{"status": login.USER}) // Your custom response here
    })
    r.Run(":8080") // listen for incoming connections
}
Thor-x86_128
  • 157
  • 18
mstafkmx
  • 419
  • 5
  • 17
  • 3
    This implies that my JSON has a user and a password fileds, it can't be any JSON. E.g. if I want a JSON like {"time":23,"space":"xyz"} this code woudn't work. I would like to receive and send raw text. Is this possible in Gin Gonic? – Salami Feb 15 '17 at 11:46
  • 2
    Why does the elements of the struct always need to begin with caps?? – Nidhin David Jul 13 '17 at 02:23
  • 2
    @NidhinDavid because you want them to be public and you want to decode them. If they are private, the JSON decoder won't be able to do this (they are not accessible to it) – Daniel Shterenberg Jul 20 '19 at 06:32
-1

I ended up creating a struct to parse my JSON, then I make some required calculations and finally I resend the data parsing my JSON to string using json.Marshal. I think it makes sense to parse the JSON, it is a way to check whether the info received is correct.

Salami
  • 317
  • 1
  • 2
  • 9