Ich versuche, einen Kommentar zu IMGUR durch die Imgur API POST. Aber ich kann die Nutzlast nicht richtig machen. Ich habe die Seite von Imgur durchgesehen und sie haben gezeigt, dass sie wie in Strings kodiert werden soll. NewReader. Die ImageID ist nP0uKKOImgur API: POST-Kommentar in GO
func HandleComment(w http.ResponseWriter, r *http.Request){
parts := strings.Split(r.URL.Path, "/")
switch r.Method {
case "POST":
// URL for POSTing comment
url := "https://api.imgur.com/3/comment"
// Authorization key
token := "Bearer " + os.Getenv("TOKEN")
// Payload
payload := strings.NewReader("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"image_id\"\r\n\r\nnP0uKKO\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"comment\"\r\n\r\nI'm a giraffe!\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--")
// Get body and http status code
body, status := DoStuff(parts[2], url, payload, token)
// If status is not OK
if status != 200 {
http.Error(w, "Could not post comment", status)
} else {
fmt.Fprintln(w, string(body))
}
DoStuff() macht dieses
func DoStuff(method string, url string, body io.Reader, key string) ([]byte, int) {
// New request
req, err := http.NewRequest(method, url, body)
if err != nil {
fmt.Println(err)
}
// Add header with 'key' authorization
req.Header.Add("authorization", key)
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
imgBody, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
}
return imgBody, res.StatusCode
}
bekomme ich nur diesen Fehler
{
data: {
error: "The parameter, image_id, is required.",
request: "/3/comment",
method: "POST"
},
success: false,
status: 400
}
Haben Sie versucht, den Content-Type als multipart/form-data zu spezifizieren? – mkopriva