// LoginJSON stuff
type LoginJSON struct {
User string `json:"user" binding:"required"`
Password string `json:"password" binding:"required"`
}
func Create(c *gin.Context) {
var json LoginJSON
c.Bind(&json) // This will infer what binder to use depending on the content-type header.
if json.User == "manu" && json.Password == "123" {
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
} else {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
}
}
the Create handler is called from a POST request. I'm using Postman to test the post calls and it's simply not recognizing it. The response is unauthorized, even when I have "manu" and "123" as my User and Password.

I have also tried c.BindWith(&json, binding.Form). Both of them do not work.
I've also tried c.PostForm("User") and that doesn't get me "manu" it just gives me an empty string. Not exactly sure how to get simple form data without and binding or validation, I just want to get the data.
@faeronsayn you are not parsing json!
type LoginForm struct {
User string `form:"user" binding:"required"`
Password string `form:"password" binding:"required"`
}
notice I changed json:"user" to form:"user"
Not exactly sure how to get simple form data without and binding or validation, I just want to get the data.
user := c.PostForm("user")
password := c.PostForm("password")
@manucorporat, not sure what i'm doing wrong but it's not working for me.
Something simple like this
func Create(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"response": c.PostForm("stuff")})
}
I'm getting {"response": ""} as my response. Here is a screenshot

@faeronsayn check out possible solutions:
1 - are you using a POST handler?
r.POST("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"response": c.PostForm("stuff")})
})
2 - are you sending correctly the post request? try this:
$ curl -X POST --data "stuff=123" localhost:8080
{"response":"123"}
@faeronsayn it works for me:

@faeronsayn I see you configured a header manually? Header (1)
can you show it to us?
@manucorporat ahh, it seems like removing the header did the trick. Slightly weird though. The header just identified the content-type to application/json. So if I'm sending whole objects as my form-data, this might not work? Here is the screenshot.

@faeronsayn of course not, because you are not sending JSON, you are sending form based data.
@faeronsayn
ahh, it seems like removing the header did the trick.
once you removed the wrong content-type, Postman was able to set the correct one under the hood.
So if I'm sending whole objects as my form-data, this might not work? Here is the screenshot.
as @javierprovecho said, there is no way it would work. form-data format is not JSON, it is a completely different data format. so, if you are declaring that the data in the body is json encoded, c.Bind() will try to decode it as a json string, this process will obviously fail returning a "invalid json syntax" error.
BTW, the content-type of form-data is multipart/form-data
Hi @manucorporat and @javierprovecho. I want to POST JSON data from the client. If I do that from the client, how can I access that JSON data with golang and gin? I know if I was simply using net/http package I could use the req.Body and use json.Decoder to get the JSON. Does gin provide similar functionality or handle this in a better fashion? Something like this:
{
"group":{
"title":"Test",
"description":"More test"
}
}
Btw, When I was trying Postman with form-data, it's not possible to get POST values
Seems it's way is not supported
err := request.ParseForm()
username := request.Form.Get("username")
Username will be always ""
But another x-www-form-urlencoded is working correct
Maybe this will help someone to detect an error
Sorry for off topic
@ovr form-data working fine without header define in Postman and using c.Bind() into the go file.
guys i have a little problem, i want to post the data and test it from postman, but the data is null on xampp

and this is my code:
`package main
import (
"bytes"
"database/sql"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/gotest")
if err != nil {
fmt.Print(err.Error())
}
defer db.Close()
err = db.Ping()
if err != nil {
fmt.Print(err.Error())
}
type Person struct {
Id int
First_Name string
Last_Name string
}
router := gin.Default()
router.POST("/person", func(c *gin.Context) {
var buffer bytes.Buffer
first_name := c.PostForm("first_name")
last_name := c.PostForm("last_name")
stmt, err := db.Prepare("insert into person (first_name, last_name) values(?,?);")
if err != nil {
fmt.Print(err.Error())
}
_, err = stmt.Exec(first_name, last_name)
if err != nil {
fmt.Print(err.Error())
}
buffer.WriteString(first_name)
buffer.WriteString(" ")
buffer.WriteString(last_name)
defer stmt.Close()
name := buffer.String()
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf(" %s successfully created", name),
})
})
router.Run(":3000")
}
`
Thanks :)
Add data binding in your "Person" struct.
type Person struct {
Id int
First_Name string `db:"first_name" form:"first_name"`
Last_Name string `db:"last_name" form:"last_name"`
}
Then get values from the form.
person:= Person{}
c.Bind(&person)
if article.First_Name != "" && person.Last_Name != "" {
stmt, err := db.Prepare(`INSERT INTO person (first_name, last_name) VALUES (?,?)`, person.First_Name, person.Last_Name)
}
@EtienneR thanks for your help sir, but i have another little problem, the error message is
db.Person undefined (type *sql.DB has no field or method Person)
and this is my code:
`package main
import (
"bytes"
"database/sql"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)
func main() {
db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/gotest")
if err != nil {
fmt.Print(err.Error())
}
defer db.Close()
err = db.Ping()
if err != nil {
fmt.Print(err.Error())
}
type Person struct {
Id int
First_Name string `db:"first_name" `
Last_Name string `db:"last_name"`
}
router := gin.Default()
router.POST("/person", func(c *gin.Context) {
var buffer bytes.Buffer
person := db.Person{}
c.Bind(&person)
if person.First_Name != "" && person.Last_Name != "" {
stmt, err := db.Prepare(`INSERT INTO person (first_name, last_name) VALUES (?,?)`)
}
if err != nil {
fmt.Print(err.Error())
}
if err != nil {
fmt.Print(err.Error())
}
name := buffer.String()
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf(" %s successfully created", name),
})
})
router.Run(":3000")
}
`
Thanks :)
person := Person{} instead of person:= db.Person{}
Most helpful comment
@manucorporat, not sure what i'm doing wrong but it's not working for me.
Something simple like this
I'm getting
{"response": ""}as my response. Here is a screenshot