Skip to main content

2 posts tagged with "http"

View All Tags

· One min read

之前遇到个问题,在一段代码中这样设置WriteHeader,在浏览器怎么样都不是json。

w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json; charset=utf-8")

后来才知道

如果这两种修改一起做,就必须让 w.WriteHeader 在所有的 w.Header.Set 之后,因为 w.WriteHeader 后 Set Header 是无效的。

所以正确的应该是

w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)

· One min read

code

package main

import (
"fmt"
"log"
"net/http"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world")
})
log.Println("server is running at http://127.0.0.1:8888")
http.ListenAndServe(":8888", nil)
}

ipv6如何访问