mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-17 15:23:44 +08:00
docs: fix example outputs and claims that differ from actual behavior
Each of these was checked by running the example against the current
master branch with net/http/httptest.
- Parameters in path: the comment said routes starting with /user/groups
are never interpreted as /user/:name/... routes. GET /user/groups/admin
is served by /user/:name/*action with name "groups". Only the exact
path /user/groups goes to the static route.
- Map as querystring or postform parameters: fmt prints map keys in
sorted order, so the output is map[a:1234 b:hello] and
map[first:thinkerou second:tianou].
- JSONP: the response body is x({"foo":"bar"}); with plain quotes and a
trailing semicolon. The escaped quotes were copied from a Go string
literal in the tests.
- Model binding: when Bind fails, AbortWithError(400, err) sends the
headers with an empty body and no Content-Type. It does not set
text/plain; charset=utf-8.
- Set and get a cookie: the first example used SetCookieData and
http.Cookie without importing net/http and time, so it did not
compile, and it duplicated the SetCookieData example that follows it.
Restore the SetCookie call it used before SetCookieData was added.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NQDmA3mwGfL83pbg9WQ6ey
This commit is contained in:
parent
0f09c3a9b4
commit
8ea227bd0a
23
docs/doc.md
23
docs/doc.md
@ -207,7 +207,8 @@ func main() {
|
||||
|
||||
// This handler will add a new router for /user/groups.
|
||||
// Exact routes are resolved before param routes, regardless of the order they were defined.
|
||||
// Routes starting with /user/groups are never interpreted as /user/:name/... routes
|
||||
// Only the exact path /user/groups is matched here. A longer path such as
|
||||
// /user/groups/admin still matches /user/:name/*action, with name set to "groups".
|
||||
router.GET("/user/groups", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "The available groups are [...]")
|
||||
})
|
||||
@ -309,7 +310,7 @@ func main() {
|
||||
```
|
||||
|
||||
```sh
|
||||
ids: map[b:hello a:1234]; names: map[second:tianou first:thinkerou]
|
||||
ids: map[a:1234 b:hello]; names: map[first:thinkerou second:tianou]
|
||||
```
|
||||
|
||||
### Upload files
|
||||
@ -866,7 +867,7 @@ Also, Gin provides two sets of methods for binding:
|
||||
|
||||
- **Type** - Must bind
|
||||
- **Methods** - `Bind`, `BindJSON`, `BindXML`, `BindQuery`, `BindYAML`, `BindHeader`, `BindTOML`
|
||||
- **Behavior** - These methods use `MustBindWith` under the hood. If there is a binding error, the request is aborted with `c.AbortWithError(400, err).SetType(ErrorTypeBind)`. This sets the response status code to 400 and the `Content-Type` header is set to `text/plain; charset=utf-8`. Note that if you try to set the response code after this, it will result in a warning `[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 422`. If you wish to have greater control over the behavior, consider using the `ShouldBind` equivalent method.
|
||||
- **Behavior** - These methods use `MustBindWith` under the hood. If there is a binding error, the request is aborted with `c.AbortWithError(400, err).SetType(ErrorTypeBind)`. This sets the response status code to 400 and writes the response headers right away, with an empty body and no `Content-Type` header. Note that if you try to set the response code after this, it will result in a warning `[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 422`. If you wish to have greater control over the behavior, consider using the `ShouldBind` equivalent method.
|
||||
- **Type** - Should bind
|
||||
- **Methods** - `ShouldBind`, `ShouldBindJSON`, `ShouldBindXML`, `ShouldBindQuery`, `ShouldBindYAML`, `ShouldBindHeader`, `ShouldBindTOML`,
|
||||
- **Behavior** - These methods use `ShouldBindWith` under the hood. If there is a binding error, the error is returned and it is the developer's responsibility to handle the request and error appropriately.
|
||||
@ -1797,7 +1798,7 @@ func main() {
|
||||
}
|
||||
|
||||
//callback is x
|
||||
// Will output : x({\"foo\":\"bar\"})
|
||||
// Will output : x({"foo":"bar"});
|
||||
c.JSONP(http.StatusOK, data)
|
||||
})
|
||||
|
||||
@ -2519,19 +2520,7 @@ func main() {
|
||||
|
||||
if err != nil {
|
||||
cookie = "NotSet"
|
||||
// Using http.Cookie struct for more control
|
||||
c.SetCookieData(&http.Cookie{
|
||||
Name: "gin_cookie",
|
||||
Value: "test",
|
||||
Path: "/",
|
||||
Domain: "localhost",
|
||||
MaxAge: 3600,
|
||||
Secure: false,
|
||||
HttpOnly: true,
|
||||
// Additional fields available in http.Cookie
|
||||
Expires: time.Now().Add(24 * time.Hour),
|
||||
// Partitioned: true, // Available in newer Go versions
|
||||
})
|
||||
c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
|
||||
}
|
||||
|
||||
fmt.Printf("Cookie value: %s \n", cookie)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user