-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.go
More file actions
37 lines (32 loc) · 756 Bytes
/
post.go
File metadata and controls
37 lines (32 loc) · 756 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"net/http"
"io/ioutil"
"fmt"
"encoding/json"
"bytes"
)
type user struct {
Id int `json:"-"`
Name interface{} `json:"username"`
Age int `json:"age"`
Email string `json:"email"`
}
func main() {
client := &http.Client{}
u := user{Id: 200, Name: "lzy", Age: 10, Email: "lizhengyin1115@163.com"}
u_json, _ := json.Marshal(u)
u_byte := []byte(u_json)
req, err := http.NewRequest("POST", "http://127.0.0.1:8080/lzy", bytes.NewBuffer(u_byte))
if err != nil {
// handle error
}
//req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
fmt.Println(string(body))
}