[[toc]]
The contracts/http/Request
method of Goravel can interact with the current HTTP request processed by the application, and get the input and files submitted together.
The http.Context
instance is automatically injected into the controller:
import "github.com/goravel/framework/contracts/http"
facades.Route().Get("/", func(ctx http.Context) {
})
path := ctx.Request().Path() // /users
url := ctx.Request().Url() // /users?name=Goravel
url := ctx.Request().Host()
url := ctx.Request().FullUrl() // http://**/users?name=Goravel
method := ctx.Request().Method()
header := ctx.Request().Header("X-Header-Name", "default")
headers := ctx.Request().Headers()
ip := ctx.Request().Ip()
You may retrieve all of the incoming request's input data as map[string]any
using the All
method, which is a collection of json
, form
and query
(priority from front to back).
data := ctx.Request().All()
// /users/{id}
id := ctx.Request().Route("id")
id := ctx.Request().RouteInt("id")
id := ctx.Request().RouteInt64("id")
// /users?name=goravel
name := ctx.Request().Query("name")
name := ctx.Request().Query("name", "default")
// /users?id=1
name := ctx.Request().QueryInt("id")
name := ctx.Request().QueryInt64("id")
name := ctx.Request().QueryBool("id")
// /users?names=goravel1&names=goravel2
names := ctx.Request().QueryArray("names")
// /users?names[a]=goravel1&names[b]=goravel2
names := ctx.Request().QueryMap("names")
queries := ctx.Request().Queries()
Note: Only one-dimensional Json data can be obtained, otherwise it will return empty.
Access all of the user input without worrying about which HTTP verb was used for the request. Retrieve order: json
, form
, query
, route
.
name := ctx.Request().Input("name")
name := ctx.Request().Input("name", "goravel")
name := ctx.Request().InputInt("name")
name := ctx.Request().InputInt64("name")
name := ctx.Request().InputBool("name")
name := ctx.Request().InputArray("name")
name := ctx.Request().InputMap("name")
type User struct {
Name string `form:"code" json:"code"`
}
var user User
err := ctx.Request().Bind(&user)
var user map[string]any
err := ctx.Request().Bind(&user)
file, err := ctx.Request().File("file")
file, err := ctx.Request().File("file")
file.Store("./public")
ctx.Request().AbortWithStatus(403)
ctx.Request().AbortWithStatusJson(403, http.Json{
"Hello": "World",
})
request := ctx.Request().Origin()
ctx.WithValue("user", "Goravel")
user := ctx.Value("user")
ctx := ctx.Context()