Skip to main content

Request

All request information like posted data, uploaded files, headers, etc; can be accessed using the context.

URL Parameters​

URL path parameters can be defined by prefixing the parameter name with :. Here is an example of defining a path parameter

file: routes.go
router.Get("users/:id", controllers.GetUsers)

Here is an example of accessing it

file controllers/users.go
func GetUsers(c *core.Context) {
userID := c.GetPathParam("id")
}

Query and form's post parameters​

Query parameters are passed to the URL like this /users?name=jack, both query parameters and the form post data can be accessed with the function GetRequestParam("PARAM_NAME"), here is an example:

func listUsers(c *core.Context) {
name := c.GetRequestParam("name")
PostParam:= c.GetRequestParam("PARAM_KEY")
}

Form uploaded files:​

Here is how you can access the form uploaded file

func saveFile(c *core.Context) {
fileInfo, err := c.GetUploadedFile("uploaded-file-key-name")
fileInfo.FullPath // full path to the temp location of the uploaded file
fileInfo.Name // the name of the file
}

Where: fileinfo is an object that has general information about the uploaded file, here is all the file information that it provides:

  • fileInfo.FullPath: full path to the temp location of the uploaded file
  • fileInfo.Name: the file name without the path
  • fileInfo.NameWithoutExtension: the file name without extension
  • fileInfo.Extension: the extension of the file
  • fileInfo.Size: the file size in bytes

Save uploaded file:​

Goffee stores all uploaded files in a temporary location, to save it to a specific location, you can simply move it using the function MoveFile(srcPath, destFolderPath), here is how:

moving uploaded file to storage dir
func SaveFile(c *core.Context) {
fileinfo, err := c.GetUploadedFile("uploaded-file-key-name")
err := c.MoveFile(fileinfo.FullPath, "./storage/files")
}

Header Data:​

Here is how you can get headers:

func ListTodos(c *core.Context) {
realIPAddress := c.GetHeader("X-Forwarded-For")
}

Client IP address:​

To obtain the IP address of the client that initiated the request, use GetRemoteIP(). It is proxy-friendly and read-only:

func LogRequest(c *core.Context) {
ip := c.GetRemoteIP()
}

The value is derived using the following precedence:

  1. The first (left-most) entry of a trusted forwarded header if present (X-Forwarded-For, then X-Real-IP). The left-most entry is the original client when the request travelled through proxies you control.
  2. Otherwise the peer address from the underlying http.Request.RemoteAddr, with the port stripped (net.SplitHostPort), gracefully handling an address that has no port.

Prefer GetRemoteIP() over reading X-Forwarded-For/X-Real-IP directly via GetHeader(), since those headers are client-supplied and spoofable when you are not behind a trusted proxy.

Raw request and metadata:​

The underlying *http.Request is exposed (read-only) for metadata the core does not wrap explicitly, such as RemoteAddr, Host, TLS state and arbitrary headers:

func Inspect(c *core.Context) {
req := c.GetRawRequest()
host := req.Host
isTLS := req.TLS != nil
}

The following convenience accessors are also available:

  • GetRawRequest(): the live, read-only *http.Request.
  • GetRemoteIP(): best-effort client IP (see above).
  • GetURL(): the full request URL (scheme, host, path and query).
  • GetPath(): the effective request path (from the live URL).
  • GetUserAgent(): the request's User-Agent header.