|
| 1 | +// |
| 2 | +// http.NewRequest() を使って HTTP POST リクエストを試してみるサンプルです。 |
| 3 | +// |
| 4 | +// リクエストの発行先は JSONPlaceholder (https://jsonplaceholder.typicode.com/) を |
| 5 | +// 使わせてもらっています。 |
| 6 | +// |
| 7 | +// REFERENCES: |
| 8 | +// - https://dev.to/devkiran/make-an-http-post-request-in-go-29p |
| 9 | +// |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "bytes" |
| 14 | + "encoding/json" |
| 15 | + "fmt" |
| 16 | + "log" |
| 17 | + "net/http" |
| 18 | + "os" |
| 19 | +) |
| 20 | + |
| 21 | +type ( |
| 22 | + Post struct { |
| 23 | + UserId int `json:"userId"` |
| 24 | + Id int `json:"id"` |
| 25 | + Title string `json:"title"` |
| 26 | + Body string `json:"body"` |
| 27 | + } |
| 28 | +) |
| 29 | + |
| 30 | +func (me *Post) String() string { |
| 31 | + return fmt.Sprintf("Uid: %d, Id: %d, Title: %s, Body: %s", me.UserId, me.Id, me.Title, me.Body) |
| 32 | +} |
| 33 | + |
| 34 | +const ( |
| 35 | + method = "POST" |
| 36 | + url = "https://jsonplaceholder.typicode.com/posts" |
| 37 | + contentType = "application/json" |
| 38 | +) |
| 39 | + |
| 40 | +var ( |
| 41 | + appLog = log.New(os.Stdout, "", 0) |
| 42 | + errLog = log.New(os.Stderr, "[Error] ", 0) |
| 43 | +) |
| 44 | + |
| 45 | +func main() { |
| 46 | + // ------------------------------------------- |
| 47 | + // Create request and set headers |
| 48 | + // ------------------------------------------- |
| 49 | + |
| 50 | + var ( |
| 51 | + body = []byte(`{"userId": 999, "title": "try-golang", "body": "try-golang/cmd/httppost"}`) |
| 52 | + buf = bytes.NewBuffer(body) |
| 53 | + ) |
| 54 | + |
| 55 | + req, err := http.NewRequest(method, url, buf) |
| 56 | + if err != nil { |
| 57 | + errLog.Println(err) |
| 58 | + return |
| 59 | + } |
| 60 | + req.Header.Add("Content-Type", contentType) |
| 61 | + |
| 62 | + // ------------------------------------------- |
| 63 | + // Send http POST request |
| 64 | + // ------------------------------------------- |
| 65 | + |
| 66 | + var ( |
| 67 | + client = &http.Client{} |
| 68 | + ) |
| 69 | + |
| 70 | + res, err := client.Do(req) |
| 71 | + if err != nil { |
| 72 | + errLog.Println(err) |
| 73 | + return |
| 74 | + } |
| 75 | + defer res.Body.Close() |
| 76 | + |
| 77 | + // ------------------------------------------- |
| 78 | + // Check http status code |
| 79 | + // ------------------------------------------- |
| 80 | + |
| 81 | + if res.StatusCode != http.StatusCreated { |
| 82 | + errLog.Printf("http status code: %d", res.StatusCode) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // ------------------------------------------- |
| 87 | + // Decode response to JSON |
| 88 | + // ------------------------------------------- |
| 89 | + |
| 90 | + var ( |
| 91 | + post = &Post{} |
| 92 | + decoder = json.NewDecoder(res.Body) |
| 93 | + ) |
| 94 | + |
| 95 | + err = decoder.Decode(post) |
| 96 | + if err != nil { |
| 97 | + errLog.Println(err) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + // ------------------------------------------- |
| 102 | + // Show results |
| 103 | + // ------------------------------------------- |
| 104 | + |
| 105 | + appLog.Printf("status: %d, resonse: %s\n", res.StatusCode, post) |
| 106 | +} |
0 commit comments