RestClient mailgun错误"消息":"'来自'参数缺失"

时间:2014-07-22 07:11:49

标签: ruby-on-rails curl rest-client mailgun

我在rails应用程序中使用RestClient发送电子邮件。 以下Curl命令有效:

    curl -s "https://api:key-XYZ@api.mailgun.net/v2/sandbox30000.mailgun.org/messages" \
    -F from='Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>' \
    -F to='me <my-email-id@gmail.com>'\
    -F subject='Hello XYZ' \
    -F text='Congratulations, you just sent an email with Mailgun!  You are truly awesome!'

但是当我使用RestClient尝试相同时,我收到以下错误消息:

    error response !!{
      "message": "'from' parameter is missing"
    }

错误消息!! 400错误请求

RestClient电话:

    RestClient::Request.execute(
    :url => "https://api:key-XYZ@api.mailgun.net/v2/sandbox0000.mailgun.org/messages",
    :method => :post,
    :from => 'Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>',
    :sender => 'Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>',
    :to => "my-email-id@gmail.com",
    :subject => "Hello XYZ",
    :text => "Text body",
    :"h:X-My-Header" => "www/mailgun-email-send",
    :verify_ssl => false)

1 个答案:

答案 0 :(得分:6)

当您使用带有多个有效负载参数和标头的Request.execute时,您需要告诉RestClient它是什么类型的数据。

您需要使用这三个修复程序来获取与RestClient相同的curl请求。

  1. 将所有帖子字段分组为有效负载哈希
  2. 将单个标头添加到标头哈希
  3. 告诉RestClient应将有效负载作为多部分发送。
  4. 更改的请求:

    RestClient::Request.execute(
      :url => "https://api:key-XYZ@api.mailgun.net/v2/sandbox0000.mailgun.org/messages",
      :method => :post,
      :payload => {
        :from => 'Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>',
        :sender => 'Mailgun Sandbox <postmaster@sandbox30000.mailgun.org>',
        :to => "my-email-id@gmail.com",
        :subject => "Hello XYZ",
        :text => "Text body",
        :multipart => true
      },
      :headers => {
        :"h:X-My-Header" => "www/mailgun-email-send"
      },
      :verify_ssl => false
    )