这里是 iris.Context 提供的完整的方法列表。

    1. type (
    2. BodyDecoder interface {
    3. Decode(data []byte) error
    4. }
    5. Unmarshaler interface {
    6. Unmarshal(data []byte, outPtr interface{}) error
    7. }
    8. UnmarshalerFunc func(data []byte, outPtr interface{}) error
    9. )
    10. func (u UnmarshalerFunc) Unmarshal(data []byte, v interface{}) error {
    11. return u(data, v)
    12. }
    • BodyDecoder

      • BodyDecoder 是一个接口,任何结构体都可以实现,以便于实现自定义读取JSON或者XML的 decode 行为。
    • 一个简单的例子:

      1. type User struct { Username string }
      2. func (u *User) Decode(data []byte) error {
      3. return json.Unmarshal(data, u)
      4. }
      • context.ReadJSON/ReadXML(&User{}) 将会调用 UserDecode来解码请求体
      • 记住:这是完全可选的,默认的ReadJSON解码器是 encoding/json ,ReadXML解码器是 encoding/xml
    • Unmarshaler

      • 这是一个接口,实现了可以反序列化任何类型的原始数据。
      • 提示:任何值的指针实现了 BodyDecoder 将会覆写 unmarshaler
    • UnmarshalerFunc
      • Unmarahsler 接口的快捷方式
      • 更多详情看 UnmarshalerBodyDecoder
    • Unmarshal

      • 解析 x-encoded 的数据,并将结构存储到 v 指向的指针中。
      • Unmarshal 使用与 Marshal 使用的相反的编码,必须为map,slice和指针。
    • Context 是一个客户端在服务器的 “中间人对象”。一个新的 Context 是从每一个连接的一个 sync.Pool中获取的。 Context 是 Iris 的HTTP流上最重要的东西。开发者通过一个 Context 发送客户端请求的响应。开发者也从Context 中获取客户端请求的信息。
    1. type Context interface {
    2. BeginRequest(http.ResponseWriter, *http.Request)
    3. EndRequest()
    4. ResetResponseWriter(ResponseWriter)
    5. Request() *http.Request
    6. ResetRequest(r *http.Request)
    7. SetCurrentRouteName(currentRouteName string)
    8. GetCurrentRoute() RouteReadOnly
    9. Do(Handlers)
    10. AddHandler(...Handler)
    11. SetHandlers(Handlers)
    12. Handlers() Handlers
    13. HandlerIndex(n int) (currentIndex int)
    14. Proceed(Handler) bool
    15. HandlerName() string
    16. HandlerFileLine() (file string, line int)
    17. RouteName() string
    18. Next()
    19. NextOr(handlers ...Handler) bool
    20. NextOrNotFound() bool
    21. NextHandler() Handler
    22. Skip()
    23. StopExecution()
    24. IsStopped() bool
    25. OnConnectionClose(fnGoroutine func()) bool
    26. OnClose(cb func())
    27. Params() *RequestParams
    28. Values() *memstore.Store
    29. Translate(format string, args ...interface{}) string
    30. Method() string
    31. Path() string
    32. RequestPath(escape bool) string
    33. Host() string
    34. Subdomain() (subdomain string)
    35. IsWWW() bool
    36. FullRequestURI() string
    37. RemoteAddr() string
    38. GetHeader(name string) string
    39. IsAjax() bool
    40. IsMobile() bool
    41. GetReferrer() Referrer
    42. Header(name string, value string)
    43. ContentType(cType string)
    44. GetContentType() string
    45. GetContentTypeRequested() string
    46. GetContentLength() int64
    47. StatusCode(statusCode int)
    48. GetStatusCode() int
    49. Redirect(urlToRedirect string, statusHeader ...int)
    50. URLParamExists(name string) bool
    51. URLParamDefault(name string, def string) string
    52. URLParam(name string) string
    53. URLParamTrim(name string) string
    54. URLParamEscape(name string) string
    55. URLParamInt(name string) (int, error)
    56. URLParamIntDefault(name string, def int) int
    57. URLParamInt32Default(name string, def int32) int32
    58. URLParamInt64(name string) (int64, error)
    59. URLParamInt64Default(name string, def int64) int64
    60. URLParamFloat64(name string) (float64, error)
    61. URLParamFloat64Default(name string, def float64) float64
    62. URLParamBool(name string) (bool, error)
    63. URLParams() map[string]string
    64. FormValueDefault(name string, def string) string
    65. FormValue(name string) string
    66. FormValues() map[string][]string
    67. PostValueDefault(name string, def string) string
    68. PostValue(name string) string
    69. PostValueTrim(name string) string
    70. PostValueInt(name string) (int, error)
    71. PostValueIntDefault(name string, def int) int
    72. PostValueInt64(name string) (int64, error)
    73. PostValueInt64Default(name string, def int64) int64
    74. PostValueFloat64(name string) (float64, error)
    75. PostValueFloat64Default(name string, def float64) float64
    76. PostValueBool(name string) (bool, error)
    77. PostValues(name string) []string
    78. FormFile(key string) (multipart.File, *multipart.FileHeader, error)
    79. UploadFormFiles(destDirectory string, before ...func(Context, *multipart.FileHeader)) (n int64, err error)
    80. NotFound()
    81. SetMaxRequestBodySize(limitOverBytes int64)
    82. GetBody() ([]byte, error)
    83. UnmarshalBody(outPtr interface{}, unmarshaler Unmarshaler) error
    84. ReadJSON(jsonObjectPtr interface{}) error
    85. ReadXML(xmlObjectPtr interface{}) error
    86. ReadForm(formObject interface{}) error
    87. ReadQuery(ptr interface{}) error
    88. Write(body []byte) (int, error)
    89. Writef(format string, args ...interface{}) (int, error)
    90. WriteString(body string) (int, error)
    91. SetLastModified(modtime time.Time)
    92. CheckIfModifiedSince(modtime time.Time) (bool, error)
    93. WriteNotModified()
    94. WriteWithExpiration(body []byte, modtime time.Time) (int, error)
    95. StreamWriter(writer func(w io.Writer) bool)
    96. ClientSupportsGzip() bool
    97. WriteGzip(b []byte) (int, error)
    98. TryWriteGzip(b []byte) (int, error)
    99. GzipResponseWriter() *GzipResponseWriter
    100. Gzip(enable bool)
    101. ViewLayout(layoutTmplFile string)
    102. ViewData(key string, value interface{})
    103. GetViewData() map[string]interface{}
    104. View(filename string, optionalViewModel ...interface{}) error
    105. Binary(data []byte) (int, error)
    106. Text(format string, args ...interface{}) (int, error)
    107. HTML(format string, args ...interface{}) (int, error)
    108. JSON(v interface{}, options ...JSON) (int, error)
    109. JSONP(v interface{}, options ...JSONP) (int, error)
    110. XML(v interface{}, options ...XML) (int, error)
    111. Markdown(markdownB []byte, options ...Markdown) (int, error)
    112. YAML(v interface{}) (int, error)
    113. ServeContent(content io.ReadSeeker, filename string, modtime time.Time, gzipCompression bool) error
    114. ServeFile(filename string, gzipCompression bool) error
    115. SendFile(filename string, destinationName string) error
    116. SetCookie(cookie *http.Cookie, options ...CookieOption)
    117. SetCookieKV(name, value string, options ...CookieOption)
    118. GetCookie(name string, options ...CookieOption) string
    119. RemoveCookie(name string, options ...CookieOption)
    120. VisitAllCookies(visitor func(name string, value string))
    121. MaxAge() int64
    122. Record()
    123. Recorder() *ResponseRecorder
    124. IsRecording() (*ResponseRecorder, bool)
    125. BeginTransaction(pipe func(t *Transaction))
    126. SkipTransactions()
    127. TransactionsSkipped() bool
    128. Exec(method, path string)
    129. RouteExists(method, path string) bool
    130. Application() Application
    131. String() string
    132. }
    • BeginRequest(http.ResponseWriter, *http.Request)
      1. - `BeginRequest` 对每个请求执行一次。
      • 它为新来的请求准备 context(新的或者从pool获取) 的字段。
      • 为了遵守 Iris 的流程,开发者应该:

        1. 1. 将处理器重置为 `nil`
        2. 2. 将值重置为空
        3. 3. session重置为 `nil`
        4. 4. 将响应writer重置为 `http.ResponseWriter`
        5. 5. 将请求重置为 `*http.Request`
      • 其他可选的步骤,视开发的应用程序类型而定
    • BeginRequest(http.ResponseWriter, *http.Request)
      1. - 当发送完响应后执行一次,当前的 `context` 无用或者释放。
      • 为了遵守 Iris 的流程,开发者应该:
        1. 刷新响应 writer 的结果
        2. 释放响应 writer
      • 其他可选的步骤,视开发的应用程序类型而定
    • ResponseWriter() ResponseWriter
      1. - 预期返回与 response writer 兼容的 `http.ResponseWriter`
    • ResetResponseWriter(ResponseWriter)
      1. - 升级或者修改 `Context` `ResponseWriter`
    • Request() *http.Request
      1. - 按预期返回原始的 `*http.Request`
    • ResetRequest(r *http.Request)
      1. - 设置 `Context` `Request`
      • 通过标准的 *http.RequestWithContext 方法创建的新请求存储到 iris.Context 中很有用。

        • 当你出于某种愿意要对 *http.Request 完全覆写时,使用 ResetRequest

        • 记住,当你只想改变一些字段的时候,你可以使用Request() ,它返回一个 Request 的指针,因此在没有完全覆写的情况下,改变也是用效的。

        • 用法:你使用原生的 http 处理器,它使用的是标准库 context 来替代 iris.Context.Values,从而获取值。

          1. r := ctx.Request()
          2. stdCtx := context.WithValue(r.Context(), key, val)
          3. ctx.ResetRequest(r.WithContext(stdCtx)).
    • SetCurrentRouteName(currentRouteName string)
      1. - `SetCurrentRouteName` 在内部设置 route 的名字, 目的是为了开发人员调用 `GetCurrentRoute()` 函数时能找到正确的当前的 `只读` 路由。
      • 它被路由器初始化,如果你手动改变名字,除了通过 GetCurrentRoute() 函数你将会获取到别的 route 之外,没有什么其它影响。
      • 此外,要在该 context 执行不同路径的处理器,你应该使用 Exec 函数,或者通过 SetHandlers/AddHandler 函数改变头部信息。
    • GetCurrentRoute() RouteReadOnly
      1. - 返回被这个请求路径注册的只读的路由。