go post json数据
代码
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const url = "http://xxxxxxxxxxxxxxxxxx"
func postData() bool {
data := make(map[string]string)
data["username"] = "hequan"
data["password"] = "password"
b, _ := json.Marshal(data)
resp, err := http.Post(url,
"application/json",
bytes.NewBuffer(b))
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
return true
}
func main() {
postData()
}
有疑问加站长微信联系(非本文作者)