服务端代码示例:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "strings"
)

type User struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

func index(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    fmt.Println("Form: ", r.Form)
    fmt.Println("Path: ", r.URL.Path)
    fmt.Println(r.Form["a"])
    fmt.Println(r.Form["b"])
    for k, v := range r.Form {
        fmt.Println(k, "=>", v, strings.Join(v, "-"))
    }
    fmt.Fprint(w, "It works !")
}

func test(w http.ResponseWriter, r *http.Request) {
    body, _ := ioutil.ReadAll(r.Body)
    //    r.Body.Close()
    body_str := string(body)
    fmt.Println(body_str)
    //    fmt.Fprint(w, body_str)
    var user User
    //    user.Name = "aaa"
    //    user.Age = 99
    //    if bs, err := json.Marshal(user); err == nil {
    //        fmt.Println(string(bs))
    //    } else {
    //        fmt.Println(err)
    //    }

    if err := json.Unmarshal(body, &user); err == nil {
        fmt.Println(user)
        user.Age += 100
        fmt.Println(user)
        ret, _ := json.Marshal(user)
        fmt.Fprint(w, string(ret))
    } else {
        fmt.Println(err)
    }
}

func main() {
    http.HandleFunc("/", index)
    http.HandleFunc("/test/", test)

    if err := http.ListenAndServe("0.0.0.0:8080", nil); err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

客户端代码示例:


package main

import (
    "fmt"
    "io/ioutil"
    //    "log"
    "net/http"
    //    "strings"
    "bytes"
    "encoding/json"
)

type User struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

func main() {
    resp, _ := http.Get("http://10.67.2.252:8080/?a=123456&b=aaa&b=bbb")
    defer resp.Body.Close()
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))

    var user User
    user.Name = "aaa"
    user.Age = 99
    if bs, err := json.Marshal(user); err == nil {
        //        fmt.Println(string(bs))
        req := bytes.NewBuffer([]byte(bs))
        tmp := `{"name":"junneyang", "age": 88}`
        req = bytes.NewBuffer([]byte(tmp))

        body_type := "application/json;charset=utf-8"
        resp, _ = http.Post("http://10.67.2.252:8080/test/", body_type, req)
        body, _ = ioutil.ReadAll(resp.Body)
        fmt.Println(string(body))
    } else {
        fmt.Println(err)
    }

    client := &http.Client{}
    request, _ := http.NewRequest("GET", "http://10.67.2.252:8080/?a=123456&b=aaa&b=bbb", nil)
    request.Header.Set("Connection", "keep-alive")
    response, _ := client.Do(request)
    if response.StatusCode == 200 {
        body, _ := ioutil.ReadAll(response.Body)
        fmt.Println(string(body))
    }

    req := `{"name":"junneyang", "age": 88}`
    req_new := bytes.NewBuffer([]byte(req))
    request, _ = http.NewRequest("POST", "http://10.67.2.252:8080/test/", req_new)
    request.Header.Set("Content-type", "application/json")
    response, _ = client.Do(request)
    if response.StatusCode == 200 {
        body, _ := ioutil.ReadAll(response.Body)
        fmt.Println(string(body))
    }
}