Go语言中设置HTTP服务请求与响应头参数的技巧

发表时间: 2024-05-16 11:54
// FileQueryHandler : 查询批量的文件元信息func FileQueryHandler(w http.ResponseWriter, r *http.Request) {    r.ParseForm()    limitCnt, _ := strconv.Atoi(r.Form.Get("limit"))    fileMetas := meta.GetLastFileMetas(limitCnt)    data, err := json.Marshal(fileMetas)    if err != nil {        w.WriteHeader(http.StatusInternalServerError)        return    }    w.Header().Set("Content-type","application/json")    w.Write(data)}......http.HandleFunc("/file/meta", handler.GetFileMetaHandler)......
//设置响应头的第二种方法w.WriteHeader(http.StatusInternalServerError)type Headerfunc (h Header) Get(key string) stringfunc (h Header) Set(key, value string)func (h Header) Add(key, value string)func (h Header) Del(key string)func (h Header) Write(w io.Writer) errorfunc (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error
const (StatusContinue = 100 // RFC 7231, 6.2.1StatusSwitchingProtocols = 101 // RFC 7231, 6.2.2StatusProcessing = 102 // RFC 2518, 10.1StatusEarlyHints = 103 // RFC 8297StatusOK = 200 // RFC 7231, 6.3.1StatusCreated = 201 // RFC 7231, 6.3.2StatusAccepted = 202 // RFC 7231, 6.3.3StatusNonAuthoritativeInfo = 203 // RFC 7231, 6.3.4StatusNoContent = 204 // RFC 7231, 6.3.5StatusResetContent = 205 // RFC 7231, 6.3.6StatusPartialContent = 206 // RFC 7233, 4.1StatusMultiStatus = 207 // RFC 4918, 11.1StatusAlreadyReported = 208 // RFC 5842, 7.1StatusIMUsed = 226 // RFC 3229, 10.4.1StatusMultipleChoices = 300 // RFC 7231, 6.4.1StatusMovedPermanently = 301 // RFC 7231, 6.4.2StatusFound = 302 // RFC 7231, 6.4.3StatusSeeOther = 303 // RFC 7231, 6.4.4StatusNotModified = 304 // RFC 7232, 4.1StatusUseProxy = 305 // RFC 7231, 6.4.5_ = 306 // RFC 7231, 6.4.6 (Unused)StatusTemporaryRedirect = 307 // RFC 7231, 6.4.7StatusPermanentRedirect = 308 // RFC 7538, 3StatusBadRequest = 400 // RFC 7231, 6.5.1StatusUnauthorized = 401 // RFC 7235, 3.1StatusPaymentRequired = 402 // RFC 7231, 6.5.2StatusForbidden = 403 // RFC 7231, 6.5.3StatusNotFound = 404 // RFC 7231, 6.5.4StatusMethodNotAllowed = 405 // RFC 7231, 6.5.5StatusNotAcceptable = 406 // RFC 7231, 6.5.6StatusProxyAuthRequired = 407 // RFC 7235, 3.2StatusRequestTimeout = 408 // RFC 7231, 6.5.7StatusConflict = 409 // RFC 7231, 6.5.8StatusGone = 410 // RFC 7231, 6.5.9StatusLengthRequired = 411 // RFC 7231, 6.5.10StatusPreconditionFailed = 412 // RFC 7232, 4.2StatusRequestEntityTooLarge = 413 // RFC 7231, 6.5.11StatusRequestURITooLong = 414 // RFC 7231, 6.5.12StatusUnsupportedMediaType = 415 // RFC 7231, 6.5.13StatusRequestedRangeNotSatisfiable = 416 // RFC 7233, 4.4StatusExpectationFailed = 417 // RFC 7231, 6.5.14StatusTeapot = 418 // RFC 7168, 2.3.3StatusMisdirectedRequest = 421 // RFC 7540, 9.1.2StatusUnprocessableEntity = 422 // RFC 4918, 11.2StatusLocked = 423 // RFC 4918, 11.3StatusFailedDependency = 424 // RFC 4918, 11.4StatusTooEarly = 425 // RFC 8470, 5.2.StatusUpgradeRequired = 426 // RFC 7231, 6.5.15StatusPreconditionRequired = 428 // RFC 6585, 3StatusTooManyRequests = 429 // RFC 6585, 4StatusRequestHeaderFieldsTooLarge = 431 // RFC 6585, 5StatusUnavailableForLegalReasons = 451 // RFC 7725, 3StatusInternalServerError = 500 // RFC 7231, 6.6.1StatusNotImplemented = 501 // RFC 7231, 6.6.2StatusBadGateway = 502 // RFC 7231, 6.6.3StatusServiceUnavailable = 503 // RFC 7231, 6.6.4StatusGatewayTimeout = 504 // RFC 7231, 6.6.5StatusHTTPVersionNotSupported = 505 // RFC 7231, 6.6.6StatusVariantAlsoNegotiates = 506 // RFC 2295, 8.1StatusInsufficientStorage = 507 // RFC 4918, 11.5StatusLoopDetected = 508 // RFC 5842, 7.2StatusNotExtended = 510 // RFC 2774, 7StatusNetworkAuthenticationRequired = 511 // RFC 6585, 6)