name: Context Module
sort: 5
Context Module
Context is a encapsulation for http request and response. Context module provides Input object for user input which is request and Output object for output which response.
Context Object
Here are the functions encapsulated for input and output in context object.
- Redirect
- Abort
- WriteString
- GetCookie
- SetCookie
Context object is the parameter of Filter function so that you can use filter to manipulate it or finish the process in advance.
Input Object
Input object is the encapsulation of request. Here are the implemented methods:
Protocol
Get request protocol. E.g.:
HTTP/1.0Uri
The RequestURI of request. E.g.:
/hiUrl
The URL of request. E.g.:
http://beego.vip/about?username=astaxieSite
The combination of scheme and domain. E.g.:
http://beego.vipScheme
The request scheme. E.g.:
http,httpsDomain
The request domain. E.g.:
beego.vipHost
The request domain. Same as Domain.
Method
The request method. It’s standard http request method. E.g.:
GET, `POST,Is
Test if it’s a http method. E.g.:
Is('GET')will return true or falseIsAjax
Test if it’s a ajax request. Return true or false.
IsSecure
Test if the request is a https request. Return true or false.
IsWebsocket
Test if the request is a Websocket request. Return true or false.
IsUpload
Test if there is file uploaded in the request. Return true or false.
IP
Return the IP of the request user. If the user is using proxy, it will get the real IP recursively.
Proxy
Return all IP addresses of the proxy request.
Refer
Return the refer of the request.
SubDomains
Return the root domain of request. For example, request domain is
blog.beego.vip, then this function returnsbeego.vip.Port
Return the port of request. E.g.: 8080
UserAgent
Return
UserAgentof request. E.g.:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36Param
Can be set in router config. Userd to get those params. E.g.:
Param(":id")return 12Query
Return all params in GET and POST requests. This is similar as
$_REQUESTin PHPHeader
Return request header. E.g.:
Header("Accept-Language")will return the value in request header, E.g.:zh-CN,zh;q=0.8,en;q=0.6Cookie
Return request Cookie. E.g.:
Cookie("username")will return the value of username in cookiesSession
You can be initialized session. It is Session object in session module of Beego. Return the related data stored in server.
Body
Return request body. E.g.: in API application request send JSON data and it can’t be retrieved by Query. You need use Body to get the JSON data.
GetData
Get value of
DatainInputSetData
Set value of
DatainInput.GetDataandSetDatais used to pass data from Filter to Controller.
Output Object
Output object is the encapsulation of request. Here are the implemented methods:
Header
Set response header. E.g.:
Header("Server","beego")Body
Set response body. E.g.:
Body([]byte("astaxie"))Cookie
Set response cookie. E.g.:
Cookie("sessionID","beegoSessionID")Json
Parse Data into JSON and call
Bodyto return it.Jsonp
Parse Data into JSONP and call
Bodyto return it.Xml
Parse Data into XML and call
Bodyto return it.Download
Pass in file path and output file.
ContentType
Set response ContentType
SetStatus
Set response status
Session
Set the value will be stored in server. E.g.:
Session("username","astaxie"). Then it can be read later.IsCachable
Test if it’s a cacheable status based on status.
IsEmpty
Test if output is empty based on status.
IsOk
Test if response is 200 based on status.
IsSuccessful
Test if response is successful based on status.
IsRedirect
Test if response is redirected based on status.
IsForbidden
Test if response is forbidden based on status.
IsNotFound
Test if response is forbidden based on status.
IsClientError
Test if response is client error based on status.
IsServerError
Test if response is server error based on status.
