请求输入

请求输入依靠 ghttp.Request 对象实现:

  1. // 客户端请求对象
  2. type Request struct {
  3. http.Request
  4. parsedGet *gtype.Bool // GET参数是否已经解析
  5. parsedPost *gtype.Bool // POST参数是否已经解析
  6. queryVars map[string][]string // GET参数
  7. routerVars map[string][]string // 路由解析参数
  8. exit *gtype.Bool // 是否退出当前请求流程执行
  9. Id int // 请求id(唯一)
  10. Server *Server // 请求关联的服务器对象
  11. Cookie *Cookie // 与当前请求绑定的Cookie对象(并发安全)
  12. Session *Session // 与当前请求绑定的Session对象(并发安全)
  13. Response *Response // 对应请求的返回数据操作对象
  14. Router *Router // 匹配到的路由对象
  15. EnterTime int64 // 请求进入时间(微秒)
  16. LeaveTime int64 // 请求完成时间(微秒)
  17. Param interface{} // 开发者自定义参数
  18. parsedHost *gtype.String // 解析过后不带端口号的服务器域名名称
  19. clientIp *gtype.String // 解析过后的客户端IP地址
  20. isFileRequest bool // 是否为静态文件请求(非服务请求,当静态文件存在时,优先级会被服务请求高,被识别为文件请求)
  21. }

ghttp.Request继承了底层的http.Request对象,并且包含了会话相关的CookieSession对象(每个请求都会有两个独立CookieSession对象)。此外,每个请求有一个唯一的Id(请求Id,全局唯一),用以标识每一个请求。此外,成员对象包含一个与当前请求对应的返回输出对象指针Response,用于数据的返回。

相关方法(API详见: godoc.org/github.com/johng-cn/gf/g/net/ghttp#Request):

  1. type Request
  2. func (r *Request) AddPost(key string, value string)
  3. func (r *Request) AddQuery(key string, value string)
  4. func (r *Request) AddRouterString(key, value string)
  5. func (r *Request) BasicAuth(user, pass string, tips ...string) bool
  6. func (r *Request) Exit()
  7. func (r *Request) IsAjaxRequest() bool
  8. func (r *Request) IsExited() bool
  9. func (r *Request) IsFileRequest() bool
  10. func (r *Request) SetPost(key string, value string)
  11. func (r *Request) SetQuery(key string, value string)
  12. func (r *Request) SetRouterString(key, value string)
  13. func (r *Request) WebSocket() (*WebSocket, error)
  14. func (r *Request) Get(key string, def ...string) string
  15. func (r *Request) GetArray(key string, def ...[]string) []string
  16. func (r *Request) GetClientIp() string
  17. func (r *Request) GetFloat32(key string, def ...float32) float32
  18. func (r *Request) GetFloat64(key string, def ...float64) float64
  19. func (r *Request) GetFloats(key string, def ...[]float64) []float64
  20. func (r *Request) GetHost() string
  21. func (r *Request) GetInt(key string, def ...int) int
  22. func (r *Request) GetInterfaces(key string, def ...[]interface{}) []interface{}
  23. func (r *Request) GetInts(key string, def ...[]int) []int
  24. func (r *Request) GetJson() *gjson.Json
  25. func (r *Request) GetMap(def ...map[string]string) map[string]string
  26. func (r *Request) GetRaw() []byte
  27. func (r *Request) GetReferer() string
  28. func (r *Request) GetString(key string, def ...string) string
  29. func (r *Request) GetStrings(key string, def ...[]string) []string
  30. func (r *Request) GetToStruct(object interface{}, mapping ...map[string]string)
  31. func (r *Request) GetUint(key string, def ...uint) uint
  32. func (r *Request) GetVar(key string, def ...interface{}) *gvar.Var
  33. func (r *Request) GetPost(key string, def ...[]string) []string
  34. func (r *Request) GetPostArray(key string, def ...[]string) []string
  35. func (r *Request) GetPostBool(key string, def ...bool) bool
  36. func (r *Request) GetPostFloat32(key string, def ...float32) float32
  37. func (r *Request) GetPostFloat64(key string, def ...float64) float64
  38. func (r *Request) GetPostFloats(key string, def ...[]float64) []float64
  39. func (r *Request) GetPostInt(key string, def ...int) int
  40. func (r *Request) GetPostInterfaces(key string, def ...[]interface{}) []interface{}
  41. func (r *Request) GetPostInts(key string, def ...[]int) []int
  42. func (r *Request) GetPostMap(def ...map[string]string) map[string]string
  43. func (r *Request) GetPostString(key string, def ...string) string
  44. func (r *Request) GetPostStrings(key string, def ...[]string) []string
  45. func (r *Request) GetPostToStruct(object interface{}, mapping ...map[string]string)
  46. func (r *Request) GetPostUint(key string, def ...uint) uint
  47. func (r *Request) GetQuery(key string, def ...[]string) []string
  48. func (r *Request) GetQueryArray(key string, def ...[]string) []string
  49. func (r *Request) GetQueryBool(key string, def ...bool) bool
  50. func (r *Request) GetQueryFloat32(key string, def ...float32) float32
  51. func (r *Request) GetQueryFloat64(key string, def ...float64) float64
  52. func (r *Request) GetQueryFloats(key string, def ...[]float64) []float64
  53. func (r *Request) GetQueryInt(key string, def ...int) int
  54. func (r *Request) GetQueryInterfaces(key string, def ...[]interface{}) []interface{}
  55. func (r *Request) GetQueryInts(key string, def ...[]int) []int
  56. func (r *Request) GetQueryMap(def ...map[string]string) map[string]string
  57. func (r *Request) GetQueryString(key string, def ...string) string
  58. func (r *Request) GetQueryStrings(key string, def ...[]string) []string
  59. func (r *Request) GetQueryToStruct(object interface{}, mapping ...map[string]string)
  60. func (r *Request) GetQueryUint(key string, def ...uint) uint
  61. func (r *Request) GetRequest(key string, def ...[]string) []string
  62. func (r *Request) GetRequestArray(key string, def ...[]string) []string
  63. func (r *Request) GetRequestBool(key string, def ...bool) bool
  64. func (r *Request) GetRequestFloat32(key string, def ...float32) float32
  65. func (r *Request) GetRequestFloat64(key string, def ...float64) float64
  66. func (r *Request) GetRequestFloats(key string, def ...[]float64) []float64
  67. func (r *Request) GetRequestInt(key string, def ...int) int
  68. func (r *Request) GetRequestInterfaces(key string, def ...[]interface{}) []interface{}
  69. func (r *Request) GetRequestInts(key string, def ...[]int) []int
  70. func (r *Request) GetRequestMap(def ...map[string]string) map[string]string
  71. func (r *Request) GetRequestString(key string, def ...string) string
  72. func (r *Request) GetRequestStrings(key string, def ...[]string) []string
  73. func (r *Request) GetRequestToStruct(object interface{}, mapping ...map[string]string)
  74. func (r *Request) GetRequestUint(key string, def ...uint) uint
  75. func (r *Request) GetRequestVar(key string, def ...interface{}) *gvar.Var
  76. func (r *Request) GetRouterArray(key string) []string
  77. func (r *Request) GetRouterString(key string) string

以上方法可以分为以下几类:

  1. Get: 常用方法,简化参数获取,GetRequestString的别名;
  2. GetQuery*: 获取GET方式传递过来的参数;
  3. GetPost*: 获取POST方式传递过来的参数;
  4. GetRequest*: 优先查找GET参数中是否有指定键名的参数,如果没有的话返回POST参数中指定键名的参数;
  5. GetRaw: 获取原始的客户端提交数据(二进制[ ]byte类型),与HTTP Method无关(注意由于是读取的请求缓冲区数据,该方法执行一次之后缓冲区便会被清空);
  6. GetJson: 自动将原始请求信息解析为gjson.Json对象返回,gjson.Json对象具体在JSON模块章节中介绍;

其中,获取的参数方法可以对指定键名的数据进行自动类型转换,例如:http://127.0.0.1:8199/?amount=19.66,通过Get/GetQueryString将会返回19.66的字符串类型,GetQueryFloat32/GetQueryFloat64将会分别返回float32和float64类型的数值19.66。但是,GetQueryInt/GetQueryUint将会返回0,而不会返回19或者20,数值的处理交给业务逻辑本身来执行