Skip to content

how to bind query string? #742

@selvam347

Description

@selvam347
package main

import "log"
import "github.com/gin-gonic/gin"

type Person struct {
	Name    string `json:"name"`
	Address string `json:"address"`
}

func main() {
	log.Println("Hello World")
	route := gin.Default()
	route.GET("/testing", startPage)
	route.Run(":8085")
}

func startPage(c *gin.Context) {
	var person Person
	if c.BindJSON(&person) == nil {
		log.Println(person.Name)
		log.Println(person.Address)
		log.Println("Binding success...............")
	} else {
		log.Println("Binding failed...............")
	}

	c.String(200, "Success")
}

The above code works only for curl -X GET localhost:8085/testing --data '{"name":"JJ", "address":"xyz"}' -H "Content-Type:application/json" but not curl -X GET localhost:8085/testing?name=JJ&address=xyz -H "Content-Type:application/json"

if i use c.Bind(&person), binding failed both case.
How can i write the above code which binds both query string and json.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions