Converting curl -F to golang

huangapple go评论77阅读模式
英文:

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-3ax6xnjp29jd6fds4gc373sgvjxteol0https://api.mailgun.net/v3/samples.mailgun.org/messagesdevs@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 &#39;api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0&#39; \ 
https://api.mailgun.net/v3/samples.mailgun.org/messages \
-F from=&#39;Excited User &lt;excited@samples.mailgun.org&gt;&#39; \
-F to=&#39;devs@mailgun.net&#39; \
-F subject=&#39;Hello&#39; \
-F text=&#39;Testing some Mailgun awesomeness!&#39;

Updated code:

    var b bytes.Buffer
w := multipart.NewWriter(&amp;b)
fw, err := w.CreateFormField(&quot;from&quot;)
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte(&quot;Excited User &lt;excited@samples.mailgun.org&gt;&quot;))
if err != nil {
log.Println(err)
}
fw, err = w.CreateFormField(&quot;to&quot;)
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte(&quot;devs@mailgun.net&quot;))
if err != nil {
log.Println(err)
}
fw, err = w.CreateFormField(&quot;subject&quot;)
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte(&quot;Hello&quot;))
if err != nil {
log.Println(err)
}
fw, err = w.CreateFormField(&quot;text&quot;)
if err != nil {
log.Println(err)
}
_, err = fw.Write([]byte(&quot;Testing some Mailgun awesomeness!&quot;))
if err != nil {
log.Println(err)
}
w.Close()
req, err := http.NewRequest(&quot;POST&quot;, &quot;https://api.mailgun.net/v3/samples.mailgun.org/messages&quot;, &amp;b)
if err != nil {
log.Println(err)
}
req.SetBasicAuth(&quot;api&quot;, &quot;key-3ax6xnjp29jd6fds4gc373sgvjxteol0&quot;)
req.Header.Add(&quot;Content-Type&quot;, w.FormDataContentType())
// req.Header.Add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
fmt.Printf(&quot;Resp: %s\n&quot;, 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

huangapple
  • 本文由 发表于 2017年6月26日 23:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/44762773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定