質問編集履歴

2

全行追記

2019/01/03 10:10

投稿

ibabababa
ibabababa

スコア13

test CHANGED
File without changes
test CHANGED
@@ -107,3 +107,115 @@
107
107
 
108
108
 
109
109
  ご教授お願いします。
110
+
111
+
112
+
113
+
114
+
115
+ ------ 追記 -------
116
+
117
+ 全行です↓
118
+
119
+ ```go
120
+
121
+ package main
122
+
123
+
124
+
125
+ import (
126
+
127
+ "bytes"
128
+
129
+ "encoding/json"
130
+
131
+ "fmt"
132
+
133
+ "net/http"
134
+
135
+ )
136
+
137
+
138
+
139
+ type Items struct {
140
+
141
+ Title string `json:"title"`
142
+
143
+ Content string `json:"content"`
144
+
145
+ Status string `json:"status"`
146
+
147
+ Categories [1]int `json:"categories"`
148
+
149
+ }
150
+
151
+
152
+
153
+ func main() {
154
+
155
+ const URL = "https://example.com/wp-json/wp/v2/posts/"
156
+
157
+ const USER = "ユーザ"
158
+
159
+ const PASS = "パス"
160
+
161
+ items := Items{Title: "title from api2", Content: "content from api", Status: "publish", Categories: [1]int{1}}
162
+
163
+
164
+
165
+ json, err := json.Marshal(&items)
166
+
167
+ if err != nil {
168
+
169
+ fmt.Print("json parce error")
170
+
171
+ }
172
+
173
+ data := []byte(json)
174
+
175
+
176
+
177
+ // Post
178
+
179
+ req, err := http.NewRequest(
180
+
181
+ "POST",
182
+
183
+ URL,
184
+
185
+ bytes.NewBuffer(data),
186
+
187
+ )
188
+
189
+ if err != nil {
190
+
191
+ fmt.Println("url scarapping failed")
192
+
193
+ }
194
+
195
+
196
+
197
+ // Content-Type 設定
198
+
199
+ req.Header.Set("Content-Type", "application/json")
200
+
201
+ req.SetBasicAuth(USER, PASS)
202
+
203
+ client := &http.Client{}
204
+
205
+ resp, err := client.Do(req)
206
+
207
+ if err != nil {
208
+
209
+ fmt.Println(err)
210
+
211
+ }
212
+
213
+ fmt.Println(resp)
214
+
215
+ defer resp.Body.Close()
216
+
217
+ }
218
+
219
+
220
+
221
+ ```

1

dataの使い道を追記

2019/01/03 10:10

投稿

ibabababa
ibabababa

スコア13

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,47 @@
48
48
 
49
49
 
50
50
 
51
+ // Post
52
+
53
+ req, err := http.NewRequest(
54
+
55
+ "POST",
56
+
57
+ API_URL,
58
+
51
- data := bytes.NewBuffer(body)
59
+ bytes.NewBuffer(data),
60
+
61
+ )
62
+
63
+ if err != nil {
64
+
65
+ fmt.Println("url scarapping failed")
66
+
67
+ }
68
+
69
+
70
+
71
+ // Content-Type 設定
72
+
73
+ req.Header.Set("Content-Type", "application/json")
74
+
75
+ req.SetBasicAuth(USER, PASS)
76
+
77
+
78
+
79
+ client := &http.Client{}
80
+
81
+ resp, err := client.Do(req)
82
+
83
+ if err != nil {
84
+
85
+ fmt.Println(err)
86
+
87
+ }
88
+
89
+ fmt.Println(resp)
90
+
91
+ defer resp.Body.Close()
52
92
 
53
93
  ```
54
94