server
func main() { // http://127.0.0.1/add?a=1&b=2 http.HandleFunc("/add", func(w http.ResponseWriter, req *http.Request) { // 1.获取参数 _ = req.ParseForm() fmt.Println("path: ", req.URL.Path) // 2.反序列化 a, _ := strconv.Atoi(req.Form["a"][0]) b, _ := strconv.Atoi(req.Form["b"][0]) w.Header().Set("Content-Type", "application/json") jData, _ := json.Marshal(map[string]int{ "data": a + b, }) _, _ = w.Write(jData) }) http.ListenAndServe(":8080", nil)}
client
func main() { // http包 client := http.Client{} res, _ := client.Get("http://localhost:8080/add?a=10&b=6") ct := res.Header.Get("Content-Type") date := res.Header.Get("Date") fmt.Println("ct: ", ct) fmt.Println("date: ", date) url := res.Request.URL code := res.StatusCode status := res.Status fmt.Println("url: ", url) fmt.Println("code: ", code) fmt.Println("status: ", status) body := res.Body readBodyStr, _ := ioutil.ReadAll(body) fmt.Println("readBodyStr: ", string(readBodyStr))}