Get、Post、Put等请求及数据返回格式 | 青训营笔记

92 阅读6分钟

Get、Post、Put等请求及数据返回格式

1. 数据请求方式的分类

所有的项目中使用的请求都遵循HTTP协议标准,HTTP协议经过了1.0和1.1两个版本的发展。

1.1 HTTP1.0

HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法。

1.2 HTTP1.1

HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。

2. Iris框架的请求处理方式

2.1 默认请求处理

Iris框架中服务实例app中包含多个方法,用来支持对上述HTTP多种请求类型的直接处理,直接定义为get方法、post方法、put方法等,app中包含的自动处理路由请求的方法与http请求类型的分类一致。

app := iris.New()
//url: http://localhost:8000/getRequest
//type:GET请求
app.Get("/getRequest", func(context context.Context) {
        path := context.Path()
        app.Logger().Info(path)
})

2.2 Handle自定义处理

除了上述1中自动处理各类别的请求外,框架还支持使用通用的Handle方法来自定义编写自己的请求处理类型及对应的方法。

//url: http://localhost:/user/info
//type:POST请求
app.Handle("POST", "/user/info", func(context context.Context) {
        context.WriteString(" User Info is Post Request , Deal is in handle func ")
})
//启动端口监听服务
app.Run(iris.Addr(":8000"))

2.3 GET请求

向特定的网络资源数据发起请求。GET请求可以携带请求数据,携带的请求数据会以?分割URL和传输数据,参数之间以&相连,比如 http://localhost:3000?name=davie&pwd=123。 如下是一个http的get类型的请求:

http://localhost:8000/userpath

服务端的路由处理方式如下:

//url:http://localhost:8000/userpath
//type:GET请求、用GET方法处理
app.Get("/userpath", func(context context.Context) {
        //获取Path
        path := context.Path()
        //日志输出
        app.Logger().Info(path)
        //写入返回数据:string类型
        context.WriteString("请求路径:" + path)
})

上述为使用已经封装的默认的app.Get方式来处理请求,使用Handle方法来进行处理,如下所示:

//url:http://localhost:8000/hello
//type:GET请求、Handle方法第一个参数为GET,表明是GET请求方式
app.Handle("GET", "/hello", func(context context.Context) {
        context.HTML("<h1> Hello world. </h1>")
})

2.4 POST请求

POST请求在进行请求时会将请求数据放在请求body中进行请求,请求数据大小没有限制。在开发过程中,我们使用postman工具来进行POST请求的调试。 POST请求的示例如下所示:

http://localhost:8000/postLogin

服务端的路由处理方式如下:

//type:POST请求
//携带数据:name、pwd命名的请求数据
app.Post("/postLogin", func(context context.Context) {
        //获取请求path
        path := context.Path()
        //日志
        app.Logger().Info(path)
        //获取请求数据字段
        name := context.PostValue("name")
        pwd, err := context.PostValueInt("pwd")
        if err != nil {
            panic(err.Error())
        }
        app.Logger().Info(name, "  ", pwd)
        //返回
        context.HTML(name)
})

上述为使用默认路由请求方法Post方法来进行处理,同时,还可以使用Handle方法来进行处理,如下图:

//url:http://localhost:8000/user/info
//type:POST请求,Handle方法第一个参数为POST,表明是Post请求
app.Handle("POST", "/user/info", func(context context.Context) {
        context.WriteString(" User Info is Post Request , Deal is in handle func ")
})

2.5 PUT、DELETE、OPTIONS、HEAD等其他类型请求

除了上述GET、POST最为常见的两种请求方式以外,还有PUT、DELETE、OPTIONS、HEAD等其他类型请求,对于其他类型的请求,如同GET和POST请求一样,都是可以通过两种方式来进行处理:

  • 1、iris框架提供的自动识别请求类型的处理请求方法,如put方法、head方法、options方法、delete方法等
  • 2、使用通用的Handle方法对路由请求进行处理,开发者自己选择具体的请求类型以、对应url和要进行处理的func。

如下是put和delete的请求处理: PUT请求

//type:PUT类型请求
app.Put("/putinfo", func(context context.Context) {
        path := context.Path()
        app.Logger().Info("请求url:", path)
})

DELETE请求

//type:DELETE类型请求  
app.Delete("/deleteuser", func(context context.Context) {
        path := context.Path()
        app.Logger().Info("Delete请求url:", path)
})

3. 请求处理的数据格式返回

在本节课程中,我们已经学习了如何对不同类型的请求进行处理以及如何获取请求所携带的数据,当后台接收到请求后,将会对请求进行处理,处理完毕后将数据返回给请求的客户端。接下来,我们看一看如何将数据进行返回,以及都有哪些形式。 在进行请求处理时,处理方法func有一个参数context。Context是用于处理请求的上下文环境变量,用于处理http请求及相关数据返回。iris框架支持多种数据格式的返回,此处我们学习掌握返回string、json、xml以及html格式的数据。

3.1 返回string类型数据

context.WriteString("hello world")

3.2 返回json格式的数据

context.JSON(iris.Map{"message": "hello word", "requestCode": 200})

3.3 返回xml格式的数据

context.XML(Person{Name: "Davie", Age: 18})

3.4 返回html格式数据

context.HTML("<h1> Davie, 12 </h1>")

通过本节课的内容,我们学习了iris框架中的不同类型的数据请求以及返回不同的数据格式。

路由功能处理方式

1. Context概念

Context是iris框架中的一个路由上下文对象,在iris框架中的源码路径定义为:{$goPath}\github.com\kataras\iris\context\context.go。以下是Context的声明和定义:

package context
type Context interface {
    BeginRequest(http.ResponseWriter, *http.Request)
    EndRequest()
    ResponseWriter() ResponseWriter
    ResetResponseWriter(ResponseWriter)
    Request() *http.Request
    SetCurrentRouteName(currentRouteName string)
    GetCurrentRoute() RouteReadOnly
    Do(Handlers)
    AddHandler(...Handler)
    SetHandlers(Handlers)
    Handlers() Handlers
    HandlerIndex(n int) (currentIndex int)
    Proceed(Handler) bool
    HandlerName() string
    Next()
    NextOr(handlers ...Handler) bool
    NextOrNotFound() bool
    NextHandler() Handler
    Skip()
    StopExecution()
    IsStopped() bool
    Params() *RequestParams
    Values() *memstore.Store
    Translate(format string, args ...interface{}) string
    Method() string
    Path() string
    RequestPath(escape bool) string
    Host() string
    Subdomain() (subdomain string)
    IsWWW() bool
    RemoteAddr() string
    GetHeader(name string) string
    IsAjax() bool
    IsMobile() bool
    Header(name string, value string)
    ContentType(cType string)
    GetContentType() string
    GetContentLength() int64
    StatusCode(statusCode int)
    GetStatusCode() int
    Redirect(urlToRedirect string, statusHeader ...int)
    URLParamExists(name string) bool
    URLParamDefault(name string, def string) string
    URLParam(name string) string
    URLParamTrim(name string) string
    URLParamEscape(name string) string
    View(filename string, optionalViewModel ...interface{}) error
    Text(text string) (int, error)
    HTML(htmlContents string) (int, error)
    JSON(v interface{}, options ...JSON) (int, error)
    JSONP(v interface{}, options ...JSONP) (int, error)
    XML(v interface{}, options ...XML) (int, error)
    Markdown(markdownB []byte, options ...Markdown) (int, error)
    ......

在该Context的接口定义中,我们可以发现,包含很多处理请求及数据返回的操作。在iris框架内,提供给开发者一个ContextPool,即存储上下文变量Context的管理池,该变量池中有多个context实例,可以进行复用。每次有新请求,就会获取一个新的context变量实例,来进行请求的路由处理。我们在实际的案例学习中,会向大家展示关于Context的相关用法。

2. 正则表达式路由

Iris框架在进行处理http请求时,支持请求url中包含正则表达式。 正则表达式的具体规则为:

  • 1、使用{}对增则表达式进行包裹,url中出现类似{}样式的格式,即识别为正则表达式
  • 2、支持自定义增则表达式的变量的命名,变量名用字母表示。比如:{name}
  • 3、支持对自定义正则表达式变量的数据类型限制,变量名和对应的数据类型之间用“:”分隔开。比如:{name:string}表示增则表达式为name,类型限定为string类型
  • 4、通过context.Params()的Get()和GetXxx()系列方法来获取对应的请求url中的增则表达式的变量
  • 5、增则表达式支持变量的数据类型包括:string、int、uint、bool等。

如下是正则表达式的请求示例:

app.Get("/api/users/{isLogin:bool}", func(context context.Context) {
    isLogin, err := context.Params().GetBool("isLogin")
    if err != nil {
        context.StatusCode(iris.StatusNonAuthoritativeInfo)
        return
    }
    if isLogin {
        context.WriteString(" 已登录 ")
    } else {
        context.WriteString(" 未登录 ")
    }
})