英文:
Converting curl -F to golang
问题
我正在尝试将用于发送电子邮件的Mailgun curl命令转换为Golang代码。这是原始的curl命令:
curl -s --user 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0' \
https://api.mailgun.net/v3/samples.mailgun.org/messages \
-F from='Excited User <excited@samples.mailgun.org>' \
-F to='devs@mailgun.net' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomeness!'
更新后的代码如下:
var b bytes.Buffer
w := multipart.NewWriter(&b)
fw, err := w.CreateFormField("from")
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte("Excited User <excited@samples.mailgun.org>"))
if err != nil {
log.Println(err)
}
fw, err = w.CreateFormField("to")
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte("devs@mailgun.net"))
if err != nil {
log.Println(err)
}
fw, err = w.CreateFormField("subject")
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte("Hello"))
if err != nil {
log.Println(err)
}
fw, err = w.CreateFormField("text")
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte("Testing some Mailgun awesomeness!"))
if err != nil {
log.Println(err)
}
w.Close()
req, err := http.NewRequest("POST", "https://api.mailgun.net/v3/samples.mailgun.org/messages", &b)
if err != nil {
log.Println(err)
}
req.SetBasicAuth("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0")
req.Header.Add("Content-Type", w.FormDataContentType())
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
fmt.Printf("Resp: %s\n", resp)
我当然已经将api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0
、https://api.mailgun.net/v3/samples.mailgun.org/messages
和devs@mailgun.net
更改为我的API密钥和电子邮件地址,但似乎不起作用。
请问有人可以帮忙看看我可能做错了什么吗?
英文:
I'm trying to convert the mailgun curl command for sending email to golang. This is the original curl:
curl -s --user 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0' \
https://api.mailgun.net/v3/samples.mailgun.org/messages \
-F from='Excited User <excited@samples.mailgun.org>' \
-F to='devs@mailgun.net' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomeness!'
Updated code:
var b bytes.Buffer
w := multipart.NewWriter(&b)
fw, err := w.CreateFormField("from")
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte("Excited User <excited@samples.mailgun.org>"))
if err != nil {
log.Println(err)
}
fw, err = w.CreateFormField("to")
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte("devs@mailgun.net"))
if err != nil {
log.Println(err)
}
fw, err = w.CreateFormField("subject")
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte("Hello"))
if err != nil {
log.Println(err)
}
fw, err = w.CreateFormField("text")
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte("Testing some Mailgun awesomeness!"))
if err != nil {
log.Println(err)
}
w.Close()
req, err := http.NewRequest("POST", "https://api.mailgun.net/v3/samples.mailgun.org/messages", &b)
if err != nil {
log.Println(err)
}
req.SetBasicAuth("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0")
req.Header.Add("Content-Type", w.FormDataContentType())
// req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
fmt.Printf("Resp: %s\n", resp)
I have of course changed api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0
, https://api.mailgun.net/v3/samples.mailgun.org/messages
and devs@mailgun.net
to my own API and email address, but it doesn't seem to work..
Could someone please take a look to see what I might be doing wrong?
答案1
得分: 1
选项-F
根据CURL文档发送一个多部分字段:
-F, --form CONTENT 指定HTTP多部分POST数据 (H)
这个链接可能对你有帮助:https://stackoverflow.com/a/20397167/1786696
英文:
Option -F
send a multipart field according to CURL documentation:
-F, --form CONTENT Specify HTTP multipart POST data (H)
This can help you: https://stackoverflow.com/a/20397167/1786696
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论