HTTP Requests
Introduction
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.
Interacting With The Request
The http.Context
instance is automatically injected into the controller:
import "github.com/goravel/framework/contracts/http"
facades.Route.Get("/", func(ctx http.Context) {
})
Retrieving The Request Path
path := ctx.Request().Path() // /users
Retrieving The Request URL
url := ctx.Request().Url() // /users?name=Goravel
Retrieving The Full Request URL
url := ctx.Request().FullUrl() // http://**/users?name=Goravel
Retrieving The Request Method
method := ctx.Request().Method()
Request Headers
header := ctx.Request().Header('X-Header-Name', 'default')
headers := ctx.Request().Headers()
Request IP Address
method := ctx.Request().Ip()
Input
Retrieving An Input Value
// /users/:id
id := ctx.Request().Input("id")
Retrieving Input From The Query String
// /users?name=goravel
name := ctx.Request().Query("name", "goravel")
// /users?names=goravel1&names=goravel2
names := ctx.Request().QueryArray("names")
// /users?names[a]=goravel1&names[b]=goravel2
names := ctx.Request().QueryMap("names")
Retrieving Form
name := ctx.Request().Form("name", "goravel")
Form Bind Struct
type User struct {
Name string `form:"code" json:"code"`
}
var user User
err := ctx.Request().Bind(&user)
File
Retrieving File
file, err := ctx.Request().File("file")
Save File
file, err := ctx.Request().File("file")
file.Store("./public")
Abort Request
ctx.Request().AbortWithStatus(403)
ctx.Request().AbortWithStatusJson(403, http.Json{
"Hello": "World",
})
Get Origin Request
request := ctx.Request().Origin()
Attach Data
ctx.WithValue("user", "Goravel")
Get Data
user := ctx.Value("user")
Get Context
ctx := ctx.Context()