GoravelGoravel
Home
Video
  • English
  • 简体中文
GitHub
Home
Video
  • English
  • 简体中文
GitHub
  • Prologue

    • Upgrade Guide

      • Upgrading To v1.15 From v1.14
      • Upgrading To v1.14 From v1.13
      • History Upgrade
    • Contribution Guide
    • Excellent Extend Packages
  • Getting Started

    • Installation
    • Configuration
    • Directory Structure
    • Compile
  • Architecture Concepts

    • Request Lifecycle
    • Service Container
    • Service Providers
    • Facades
  • The Basics

    • Routing
    • HTTP Middleware
    • Controllers
    • Requests
    • Responses
    • Views
    • Grpc
    • Session
    • Validation
    • Logging
  • Digging Deeper

    • Artisan Console
    • Cache
    • Events
    • File Storage
    • Mail
    • Queues
    • Task Scheduling
    • Localization
    • Package Development
    • Color
    • Strings
    • Helpers
  • Security

    • Authentication
    • Authorization
    • Encryption
    • Hashing
  • ORM

    • Getting Started
    • Relationships
    • Migrations
    • Seeding
    • Factories
  • Testing

    • Getting Started
    • HTTP Tests
    • Mock

HTTP Requests

  • Introduction
  • Interacting With The Request
    • Retrieving The Request Path
    • Retrieving The Request URL
    • Retrieving The Request HOST
    • Retrieving The Full Request URL
    • Retrieving The Request Method
    • Request Headers
    • Request IP Address
  • Input
    • Retrieving All Input Data
    • Retrieving a Route Value
    • Retrieving Input From The Query String
    • Retrieving An Input Value
    • Bind Json/Form
    • Bind Query
  • Cookie
    • Retrieving a Cookie Value
  • File
    • Retrieving File
    • Save File
    • Get Origin Request
    • Attach Data
    • Get Data
    • Get Context
  • Custom Recovery

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 Request HOST

url := ctx.Request().Host()

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

ip := ctx.Request().Ip()

Input

Retrieving All Input Data

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()

Retrieving a Route Value

// /users/{id}
id := ctx.Request().Route("id")
id := ctx.Request().RouteInt("id")
id := ctx.Request().RouteInt64("id")

Retrieving Input From The Query String

// /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.

Retrieving An Input Value

Access all of the user input without worrying about which HTTP verb was used for the request. Retrieve order: json, form.

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")

Bind Json/Form

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)

Bind Query

Only support bind Query to struct:

type Test struct {
  ID string `form:"id"`
}
var test Test
err := ctx.Request().BindQuery(&test)

Cookie

Retrieving a Cookie Value

Goravel provides a simple way to work with cookie. Use the Cookie method on the Request instance to retrieve a cookie value, will return an empty string if the cookie is not present. You can also define a default value in the second argument.

value := ctx.Request().Cookie("name")
value := ctx.Request().Cookie("name", "default") 

File

Retrieving File

file, err := ctx.Request().File("file")

Save File

file, err := ctx.Request().File("file")
file.Store("./public")

Get Origin Request

request := ctx.Request().Origin()

Attach Data

ctx.WithValue("user", "Goravel")

Get Data

user := ctx.Value("user")

Get Context

ctx := ctx.Context()

Custom Recovery

You can set a custom recovery by calling the Recover method in the app/providers/route_service_provider.go file.

// app/providers/route_service_provider.go
func (receiver *RouteServiceProvider) Boot(app foundation.Application) {
  // Add HTTP middleware
  facades.Route().GlobalMiddleware(http.Kernel{}.Middleware()...)
  facades.Route().Recover(func(ctx http.Context, err error) {
    ctx.Request().Abort()
    // or
    // ctx.Response().String(500, "Internal Server Error").Abort()
  })
  ...
}
Edit this page
Prev
Controllers
Next
Responses