Skip to content

Commit afbbcf4

Browse files
author
Oleg Lobanov
committed
fix media compatibility
1 parent 0f4f3dc commit afbbcf4

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ _testmain.go
2323
*.test
2424
*.prof
2525
credential.txt
26+
27+
.idea

client.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/parnurzeal/gorequest"
77
"io/ioutil"
88
"log"
9+
"mime/multipart"
910
"net/http"
1011
"reflect"
1112
)
@@ -193,14 +194,18 @@ func (client *Client) PostData(url string, content []byte, contentType string, f
193194
// so, we have to manually create a HTTP client
194195
s := client.req.Post(url)
195196

196-
buf := bytes.NewBuffer(content)
197+
var buf bytes.Buffer
198+
w := multipart.NewWriter(&buf)
199+
fileField, _ := w.CreateFormFile("file", filename)
200+
fileField.Write(content)
201+
w.Close()
197202

198-
req, err := http.NewRequest(s.Method, s.Url, buf)
203+
req, err := http.NewRequest(s.Method, s.Url, &buf)
199204
if err != nil {
200205
return nil, nil, err
201206
}
202207

203-
req.Header.Set("Content-Type", contentType)
208+
req.Header.Set("Content-Type", w.FormDataContentType())
204209
req.Header.Set("Content-Disposition", fmt.Sprintf("filename=%v", filename))
205210

206211
// Add basic auth

media.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ type Media struct {
4949
MediaStatus string `json:"comment_status,omitempty"`
5050
PingStatus string `json:"ping_status,omitempty"`
5151
AltText string `json:"alt_text,omitempty"`
52-
Caption string `json:"caption,omitempty"`
53-
Description string `json:"description,omitempty"`
52+
Caption Caption `json:"caption,omitempty"`
53+
Description Description `json:"description,omitempty"`
5454
MediaType string `json:"media_type,omitempty"`
5555
MediaDetails MediaDetails `json:"media_details,omitempty"`
5656
Post int `json:"post,omitempty"`

posts.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,25 @@ type Excerpt struct {
5252
Rendered string `json:"rendered,omitempty"`
5353
}
5454

55+
type Description struct {
56+
Raw string `json:"raw,omitempty"`
57+
Rendered string `json:"rendered,omitempty"`
58+
}
59+
60+
type Caption struct {
61+
Raw string `json:"raw,omitempty"`
62+
Rendered string `json:"rendered,omitempty"`
63+
}
64+
5565
type Post struct {
5666
collection *PostsCollection `json:"-,omitempty"`
5767

5868
ID int `json:"id,omitempty"`
5969
Date string `json:"date,omitempty"`
6070
DateGMT string `json:"date_gmt,omitempty"`
6171
GUID GUID `json:"guid,omitempty"`
72+
FeaturedMedia int `json:"featured_media"`
73+
Categories []int `json:"categories"`
6274
Link string `json:"link,omitempty"`
6375
Modified string `json:"modified,omitempty"`
6476
ModifiedGMT string `json:"modifiedGMT,omitempty"`
@@ -70,7 +82,6 @@ type Post struct {
7082
Content Content `json:"content,omitempty"`
7183
Author int `json:"author,omitempty"`
7284
Excerpt Excerpt `json:"excerpt,omitempty"`
73-
FeaturedImage int `json:"featured_image,omitempty"`
7485
CommentStatus string `json:"comment_status,omitempty"`
7586
PingStatus string `json:"ping_status,omitempty"`
7687
Format string `json:"format,omitempty"`

0 commit comments

Comments
 (0)