-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to bind query string? #742
Comments
+1 for c.BindQuery |
We don't need Try type Person struct {
Name string `form:"name"`
Address string `form:"address"`
} Try type Person struct {
Name string `json:"name"`
Address string `json:"address"`
} |
Looking over the binding code, I don't see anywhere that it accesses req.URL.Query(). I don't understand how we can bind query parameters without that. |
@ei-grad Thanks, that was non-obvious :-) |
The following example is working for me. package main
import "log"
import "github.com/gin-gonic/gin"
type Person struct {
Name string `form:"name" json:"name"`
Address string `form:"address" json:"address"`
}
func main() {
route := gin.Default()
route.GET("/testing", startPage)
route.Run(":8085")
}
func startPage(c *gin.Context) {
var person Person
if c.Bind(&person) == nil {
log.Println("====== Bind By Query String ======")
log.Println(person.Name)
log.Println(person.Address)
}
if c.BindJSON(&person) == nil {
log.Println("====== Bind By JSON ======")
log.Println(person.Name)
log.Println(person.Address)
}
c.String(200, "Success")
} And try the following command: # bind by query
$ curl -X GET "localhost:8085/testing?name=appleboy&address=xyz"
# bind by json
$ curl -X GET localhost:8085/testing --data '{"name":"JJ", "address":"xyz"}' -H "Content-Type:application/json" You can find the example from the following URL: https://github.com/gin-gonic/gin#model-binding-and-validation Maybe we need to add bind query example on documents. 😄 |
It would be great. |
What if you posted values and had query string params how would you only parse/bind the query string params. Bind always uses r.Form which is a combination of posted values and query params...same goes for only wanting to parse/bind r.PostForm and not wanting query string. |
+1 for BindQuery |
@mehdy See the comment #742 (comment) |
Signed-off-by: Bo-Yi Wu <[email protected]>
I make new PR to improve document. #772 |
So @appleboy if you posted data and had Query string params how could you ONLY bind the Query string params or ONLY bind posted data? It seems like my previous comment was just overlooked. |
Do you see the screenshot? only bind the query or post data if c.Bind(&person) == nil {
log.Println("====== Bind By Query String ======")
log.Println(person.Name)
log.Println(person.Address)
} only bind the json data if c.BindJSON(&person) == nil {
log.Println("====== Bind By JSON ======")
log.Println(person.Name)
log.Println(person.Address)
} |
Yes @appleboy I did see the screenshot, your example is for Query params and JSON and my example was posting Form Data and Query params. What if someone wanted to ONLY bind the Query Params and not the form data. |
that's exactly the point! I think we need an |
Only Bind Query StringSee PR #1029. Add package main
import "log"
import "github.com/gin-gonic/gin"
type Person struct {
Name string `form:"name"`
Address string `form:"address"`
}
func main() {
route := gin.Default()
route.Any("/testing", startPage)
route.Run(":8085")
}
func startPage(c *gin.Context) {
var person Person
if c.BindQuery(&person) == nil {
log.Println("====== Only Bind Query String ======")
log.Println(person.Name)
log.Println(person.Address)
}
c.String(200, "Success")
} Try the following command: # only bind query
$ curl -X GET "localhost:8085/testing?name=eason&address=xyz"
# only bind query string, ignore form data
$ curl -X POST "localhost:8085/testing?name=eason&address=xyz" --data 'name=ignore&address=ignore' -H "Content-Type:application/x-www-form-urlencoded"
|
#1029 merged |
bindQuery also works with "form" tag |
does this only work with I think |
I arrived here from this URL, and then this reproduces it, and finds that it is different from the above output Whether I send a request with query or json, he recognizes it as a json type. $ curl -X GET localhost:8085/testing --data '{"name":"JJ", "address":"xyz"}' -H "Content-Type:application/json"
Successcurl: (3) unmatched close brace/bracket in URL position 12:
address:xyz}'
^
and now I ignored the argument and passed an empty piece of data: $ curl -X GET localhost:8085/testing --data "" -H "Content-Type:application/json"
Success but in the terminal:
Is anyone having the same problem as me? |
There are serveral things wrong here:
Hope this could help you |
The above code works only for
curl -X GET localhost:8085/testing --data '{"name":"JJ", "address":"xyz"}' -H "Content-Type:application/json"
but notcurl -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.
The text was updated successfully, but these errors were encountered: