packagemainimport("fmt""io""log""net/http")funcmain(){response,err:=http.Get("http://httpbin.org/get")iferr!=nil{log.Fatal(err)}fmt.Println(response.Status)fmt.Println(response.Proto)fmt.Println("------------请求头----------------")fork,v:=rangeresponse.Header{fmt.Printf("%s:%s\n",k,v[0])}body,err:=io.ReadAll(response.Body)deferresponse.Body.Close()ifresponse.StatusCode!=http.StatusOK{log.Fatalf("Response failed with status code: %d\n",response.StatusCode)}iferr!=nil{log.Fatal(err)}fmt.Println("---------请求体--------")fmt.Println(string(body))fmt.Println("--------响应长度-------")fmt.Println(response.ContentLength)}
packagemainimport("encoding/json""fmt""io""log""net/http""strings")funcmain(){data:=make(map[string]string,0)data["key"]="001"buf,err:=json.Marshal(data)iferr!=nil{log.Fatal(err)}reqBody:=strings.NewReader(string(buf))res,err:=http.Post("http://httpbin.org/post","application/json",reqBody)iferr!=nil{log.Fatal(err)}body,err:=io.ReadAll(res.Body)deferres.Body.Close()ifres.StatusCode!=http.StatusOK{log.Fatalf("Response failed with status code: %d\n",res.StatusCode)}fmt.Printf("%s",body)}
packagemainimport("fmt""io""log""net/http""net/url")funcmain(){res,err:=http.PostForm("http://httpbin.org/post",url.Values{"key1":{"001"},"key2":{"002"}})iferr!=nil{log.Fatalln(err)}body,err:=io.ReadAll(res.Body)deferres.Body.Close()ifres.StatusCode!=http.StatusOK{log.Fatalf("Response failed with status code: %d\n",res.StatusCode)}fmt.Printf("%s",body)}