英文:
Passing Audio/ Video File to API
问题
我正在尝试使用Soundcloud API(https://developers.soundcloud.com/docs/api/reference#tracks)将音频文件上传到Soundcloud。我必须传递文件的参数需要"音频文件的二进制数据",但我不确定如何在Go中加载这样的数据。
我的当前代码如下,但是音频文件当然无法正确发送。
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
label, err := w.CreateFormField("oauth_token")
if err != nil {
return err
}
label.Write([]byte(c.Token.AccessToken))
fw, err := w.CreateFormFile("upload", "platform/young.mp3")
if err != nil {
return err
}
fd, err := os.Open("platform/young.mp3")
if err != nil {
return err
}
defer fd.Close()
_, err = io.Copy(fw, fd)
if err != nil {
return err
}
w.Close()
req, err := http.NewRequest("POST", "https://api.soundcloud.com/tracks.json", buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.SetBasicAuth("email@email.com", "password")
fmt.Println(req.Form)
res, err := c.Client.Do(req)
if err != nil {
return err
}
英文:
I'm trying to use the Soundcloud API (https://developers.soundcloud.com/docs/api/reference#tracks) to upload an audio file to Soundcloud. The parameter I must pass the file in requires "binary data of the audio file" and I'm unsure how to load such a thing in Go.
My current code is as follows, but the audio file of course does not send properly.
buf := new(bytes.Buffer)
w := multipart.NewWriter(buf)
label, err := w.CreateFormField("oauth_token")
if err != nil {
return err
}
label.Write([]byte(c.Token.AccessToken))
fw, err := w.CreateFormFile("upload", "platform/young.mp3")
if err != nil {
return err
}
fd, err := os.Open("platform/young.mp3")
if err != nil {
return err
}
defer fd.Close()
_, err = io.Copy(fw, fd)
if err != nil {
return err
}
w.Close()
req, err := http.NewRequest("POST", "https://api.soundcloud.com/tracks.json", buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", w.FormDataContentType())
req.SetBasicAuth("email@email.com", "password")
fmt.Println(req.Form)
res, err := c.Client.Do(req)
if err != nil {
return err
}
答案1
得分: 1
我还没有测试下面的代码,因为我没有有效的Oauth令牌,但它可能会帮助你找到正确的方向。
package main
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
func main() {
uri := "https://api.soundcloud.com/tracks.json"
params := map[string]string{
"oauth_token": "************",
"track[title]": "Test Track",
"track[sharing]": "public",
}
trackData := "track[asset_data]"
path := "test_track.mp3"
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(trackData, filepath.Base(path))
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(part, file)
for key, val := range params {
err := writer.WriteField(key, val)
if err != nil {
log.Fatal(err)
}
}
err = writer.Close()
if err != nil {
log.Fatal(err)
}
request, err := http.NewRequest("POST", uri, body)
if err != nil {
log.Fatal(err)
}
request.Header.Set("Content-Type", writer.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
log.Fatal(err)
} else {
body := &bytes.Buffer{}
_, err := body.ReadFrom(resp.Body)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header)
fmt.Println(body)
}
}
希望对你有帮助!
英文:
I haven't tested the code below, as I don't have a valid Oauth token, but it may put you on the right track.
package main
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
func main() {
uri := "https://api.soundcloud.com/tracks.json"
params := map[string]string{
"oauth_token": "************",
"track[title]": "Test Track",
"track[sharing]": "public",
}
trackData := "track[asset_data]"
path := "test_track.mp3"
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(trackData, filepath.Base(path))
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(part, file)
for key, val := range params {
err := writer.WriteField(key, val)
if err != nil {
log.Fatal(err)
}
}
err = writer.Close()
if err != nil {
log.Fatal(err)
}
request, err := http.NewRequest("POST", uri, body)
if err != nil {
log.Fatal(err)
}
request.Header.Set("Content-Type", writer.FormDataContentType())
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
log.Fatal(err)
} else {
body := &bytes.Buffer{}
_, err := body.ReadFrom(resp.Body)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header)
fmt.Println(body)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论