goravel
  • README
  • ORM
    • getting-started
    • Migrations
    • Relationships
  • Architecutre Concepts
    • Facades
    • Request Lifecycle
    • Service Container
    • Service Providers
  • Digging Deeper
    • Artisan Console
    • Cache
    • Events
    • File Storage
    • Helpers
    • Mail
    • Mock
    • Package Development
    • Queues
    • Task Scheduling
  • Getting Started
    • Compile
    • Configuration
    • Directory Structure
    • Installation
  • prologue
    • Contribution Guide
    • Excellent Extend Packages
  • security
    • Authentication
    • Authorization
    • Encryption
    • Hashing
  • The Basics
    • Controllers
    • Grpc
    • Logging
    • Middleware
    • HTTP Requests
    • HTTP Response
    • Routing
    • Validation
  • upgrade
    • History Upgrade
    • Upgrading To v1.1 From v1.0
    • Upgrading To v1.10 From v1.9
    • Upgrading To v1.11 From v1.10
    • Upgrading To v1.12 From v1.11
    • Upgrading To v1.2 From v1.1
    • Upgrading To v1.3 From v1.2
    • Upgrading To v1.4 From v1.3
    • Upgrading To v1.5 From v1.4
    • Upgrading To v1.6 From v1.5
    • Upgrading To v1.7 From v1.6
    • Upgrading To v1.8 From v1.7
    • Upgrading To v1.9 From v1.8
  • zh
    • ORM
      • 快速入门
      • 数据库迁移
      • 模型关联
    • 核心架构
      • Facades
      • 请求周期
      • 服务容器
      • 服务提供者
    • 综合话题
      • Artisan 命令行
      • 缓存系统
      • 事件系统
      • 文件存储
      • 辅助函数
      • 发送邮件
      • Mock
      • 扩展包开发
      • 队列
      • 任务调度
    • 入门指南
      • 编译
      • 配置信息
      • 文件夹结构
      • 安装
    • prologue
      • 贡献指南
      • 优秀扩展包
    • security
      • 用户认证
      • 用户授权
      • 加密解密
      • 哈希
    • 基本功能
      • 控制器
      • Grpc
      • 日志
      • HTTP 中间件
      • 请求
      • 响应
      • 路由
      • 表单验证
    • upgrade
      • 历史版本升级
      • 从 v1.0 升级到 v1.1
      • 从 v1.9 升级到 v1.10
      • 从 v1.10 升级到 v1.11
      • 从 v1.11 升级到 v1.12
      • 从 v1.1 升级到 v1.2
      • 从 v1.2 升级到 v1.3
      • 从 v1.3 升级到 v1.4
      • 从 v1.4 升级到 v1.5
      • 从 v1.5 升级到 v1.6
      • 从 v1.6 升级到 v1.7
      • 从 v1.7 升级到 v1.8
      • 从 v1.8 升级到 v1.9
Powered by GitBook
On this page
  • 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 An Route Value
  • Retrieving Input From The Query String
  • Retrieving Form
  • Retrieving Json
  • Retrieving An Input Value
  • Json/Form Bind Struct
  • File
  • Retrieving File
  • Save File
  • Abort Request
  • Get Origin Request
  • Attach Data
  • Get Data
  • Get Context
Edit on GitHub
  1. The Basics

HTTP Requests

[[toc]]

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

method := 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, is a collection of json, form and query(priority from front to back).

data := ctx.Request().All()

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

Retrieving Form

name := ctx.Request().Form("name")
name := ctx.Request().Form("name", "goravel")

Retrieving Json

name := ctx.Request().Json("name")
name := ctx.Request().Json("name", "goravel")

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, query, route.

name := ctx.Request().Input("name")
name := ctx.Request().Json("name", "goravel")
name := ctx.Request().InputInt("name")
name := ctx.Request().InputInt64("name")
name := ctx.Request().InputBool("name")

Json/Form Bind Struct

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

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

Last updated 1 year ago