内容渲染
发布者:admin 发表于:436天前 阅读数:540 评论:0

Web处理程序可以返回各种内容类型,例如,JSON,纯文本,图像等。 通常在与API通信时,可以指定并接收内容类型,以阐明将传入数据的格式以及要接收的数据。

本节将通过第三方库对数据格式进行切换。

实践

获取第三方库:

go get github.com/unrolled/render

建立 negotiate.go:

package negotiate

import (
    "net/http"

    "github.com/unrolled/render"
)

// Negotiator 封装render并对ContentType进行一些切换
type Negotiator struct {
    ContentType string
    *render.Render
}

// GetNegotiator 接收http请求 并从Content-Type标头中找出ContentType
func GetNegotiator(r *http.Request) *Negotiator {
    contentType := r.Header.Get("Content-Type")

    return &Negotiator{
        ContentType: contentType,
        Render:      render.New(),
    }
}

建立 respond.go:

package negotiate

import "io"
import "github.com/unrolled/render"

// Respond 根据 Content Type 判断应该返回什么样类型的数据
func (n *Negotiator) Respond(w io.Writer, status int, v interface{}) {
    switch n.ContentType {
    case render.ContentJSON:
        n.Render.JSON(w, status, v)
    case render.ContentXML:
        n.Render.XML(w, status, v)
    default:
        n.Render.JSON(w, status, v)
    }
}

建立 handler.go:

package negotiate

import (
    "encoding/xml"
    "net/http"
)

// Payload 甚至数据模板
type Payload struct {
    XMLName xml.Name `xml:"payload" json:"-"`
    Status  string   `xml:"status" json:"status"`
}

// Handler 调用GetNegotiator处理返回格式
func Handler(w http.ResponseWriter, r *http.Request) {
    n := GetNegotiator(r)

    n.Respond(w, http.StatusOK, &Payload{Status: "Successful!"})
}

建立 main.go:

package main

import (
    "fmt"
    "net/http"

    "github.com/agtorre/go-cookbook/chapter7/negotiate"
)

func main() {
    http.HandleFunc("/", negotiate.Handler)
    fmt.Println("Listening on port :3333")
    err := http.ListenAndServe(":3333", nil)
    panic(err)
}

运行:

$ go run main.go
Listening on port :3333

$curl "http://localhost:3333 -H "Content-Type: text/xml"
Successful!
$curl "http://localhost:3333 -H "Content-Type: application/json"
{"status":"Successful!"}

说明

github.com/unrolled/render 包可以帮助你处理各种类型的请求头。请求头通常包含多个值,您的代码必须考虑到这一点。