当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


GO ResponseWriter用法及代码示例


GO语言"net/http"包中"ResponseWriter"类型的用法及代码示例。

HTTP 处理程序使用ResponseWriter 接口来构造 HTTP 响应。

在 Handler ServeHTTP 方法返回后,不能使用 ResponseWriter。

用法:

type ResponseWriter interface {
    // Header returns the header map that will be sent by
    // WriteHeader.The Header map also is the mechanism with which
    // Handlers can set HTTP trailers.
    //
    // Changing the header map after a call to WriteHeader(or
    // Write) has no effect unless the modified headers are
    // trailers.
    //
    // There are two ways to set Trailers.The preferred way is to
    // predeclare in the headers which trailers you will later
    // send by setting the "Trailer" header to the names of the
    // trailer keys which will come later.In this case, those
    // keys of the Header map are treated as if they were
    // trailers.See the example.The second way, for trailer
    // keys not known to the Handler until after the first Write,
    // is to prefix the Header map keys with the TrailerPrefix
    // constant value.See TrailerPrefix.
    //
    // To suppress automatic response headers(such as "Date"), set
    // their value to nil.
    Header() Header

    // Write writes the data to the connection as part of an HTTP reply.
    //
    // If WriteHeader has not yet been called, Write calls
    // WriteHeader(http.StatusOK) before writing the data.If the Header
    // does not contain a Content-Type line, Write adds a Content-Type set
    // to the result of passing the initial 512 bytes of written data to
    // DetectContentType.Additionally, if the total size of all written
    // data is under a few KB and there are no Flush calls, the
    // Content-Length header is added automatically.
    //
    // Depending on the HTTP protocol version and the client, calling
    // Write or WriteHeader may prevent future reads on the
    // Request.Body.For HTTP/1.x requests, handlers should read any
    // needed request body data before writing the response.Once the
    // headers have been flushed(due to either an explicit Flusher.Flush
    // call or writing enough data to trigger a flush), the request body
    // may be unavailable.For HTTP/2 requests, the Go HTTP server permits
    // handlers to continue to read the request body while concurrently
    // writing the response.However, such behavior may not be supported
    // by all HTTP/2 clients.Handlers should read before writing if
    // possible to maximize compatibility.
    Write([]byte)(int, error)

    // WriteHeader sends an HTTP response header with the provided
    // status code.
    //
    // If WriteHeader is not called explicitly, the first call to Write
    // will trigger an implicit WriteHeader(http.StatusOK).
    // Thus explicit calls to WriteHeader are mainly used to
    // send error codes.
    //
    // The provided code must be a valid HTTP 1xx-5xx status code.
    // Only one header may be written.Go does not currently
    // support sending user-defined 1xx informational headers,
    // with the exception of 100-continue response header that the
    // Server sends automatically when the Request.Body is read.
    WriteHeader(statusCode int)
}

示例(预告片):

HTTP Trailers 是一组键/值对,例如 HTTP 响应之后而不是之前的标头。

package main

import (
	"io"
	"net/http"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/sendstrailers", func(w http.ResponseWriter, req *http.Request) {
		// Before any call to WriteHeader or Write, declare
		// the trailers you will set during the HTTP
		// response. These three headers are actually sent in
		// the trailer.
		w.Header().Set("Trailer", "AtEnd1, AtEnd2")
		w.Header().Add("Trailer", "AtEnd3")

		w.Header().Set("Content-Type", "text/plain; charset=utf-8") // normal header
		w.WriteHeader(http.StatusOK)

		w.Header().Set("AtEnd1", "value 1")
		io.WriteString(w, "This HTTP response has both headers before this text and trailers at the end.\n")
		w.Header().Set("AtEnd2", "value 2")
		w.Header().Set("AtEnd3", "value 3") // These will appear as trailers.
	})
}

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 ResponseWriter。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。