如何让HTTParty识别“:footer”和“:collector”参数?

时间:2013-09-11 23:14:08

标签: ruby curl httparty

我从开发团队收到以下代码:

curl -u EMAILADDRESS:PASSWORD -d "sender=NAME <EMAILADDRESS>&message=[Invite Link]&collector=COLLECTOR&subject=Test Invite&footer=My Custom Text [Unsubscription Link]"

我被告知上述工作正常。这是我在Ruby 1.9.3中使用httparty gem:

将其翻译成的内容
call= "/api/v2/emails/?survey=#{i}"    
puts collector_final_id
url= HTTParty.post("https://www.fluidsurveys.com#{call}",
  :basic_auth => auth,
  :headers => { 'Content-Type' => 'application/x-www-form-urlencoded','Accept' => 'application/x-www-form-urlencoded' },
  :collector => collector,
  :body => {
    "subject" => "Test Invite",
    "sender" => "NAME <EMAILADDRESS>",
    "message" => "[Invite Link]"
  },
  :footer => "My Custom Text [Unsubscription Link]"
)

:footer:collector参数外,其中的所有内容都可以正常工作。它似乎根本不认识它们。

没有抛出任何错误,它们只是不包含在我发送的实际电子邮件中。传递这两个参数时我做错了什么?

2 个答案:

答案 0 :(得分:0)

:body参数

后面没有逗号

答案 1 :(得分:0)

您的:collector:footer不正确。

我写了一个小的Sinatra服务来接收带有任何参数的POST请求:

require 'pp'
require 'sinatra'

post "/*" do
  pp params
end

然后运行它,在我的Mac OS笔记本电脑上启动网络服务器。正如Sinatra应用程序所做的那样,它位于0.0.0.0:4567。

运行此代码:

require 'httparty'

url = HTTParty.post(
  "http://localhost:4567/api/v2/emails?survey=1",
  :headers => {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Accept' => 'application/x-www-form-urlencoded'
  },
  :body => {
    "subject" => 'subject',
    "sender" => 'sender',
    "message" => 'message',
  },
  :collector => 'collector',
  :footer => 'footer'
)

puts url

输出:

["survey", "1"]["subject", "subject"]["sender", "sender"]["message", "message"]["splat", ["api/v2/emails"]]["captures", ["api/v2/emails"]]
西纳特拉说:

127.0.0.1 - - [11/Sep/2013 17:58:47] "POST /api/v2/emails?survey=1 HTTP/1.1" 200 - 0.0163
{"survey"=>"1",
 "subject"=>"subject",
 "sender"=>"sender",
 "message"=>"message",
 "splat"=>["api/v2/emails"],
 "captures"=>["api/v2/emails"]}

:collector:footer更改为字符串并将其移动到正文中,它们应该位于:

require 'httparty'

url = HTTParty.post(
  "http://localhost:4567/api/v2/emails?survey=1",
  :headers => {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Accept' => 'application/x-www-form-urlencoded'
  },
  :body => {
    "subject" => 'subject',
    "sender" => 'sender',
    "message" => 'message',
    'collector' => 'collector',
    'footer' => 'footer'
  },
)

puts url

输出:

["survey", "1"]["subject", "subject"]["sender", "sender"]["message", "message"]["collector", "collector"]["footer", "footer"]["splat", ["api/v2/emails"]]["captures", ["api/v2/emails"]]

Sinatra说:

127.0.0.1 - - [11/Sep/2013 18:04:13] "POST /api/v2/emails?survey=1 HTTP/1.1" 200 - 0.0010
{"survey"=>"1",
 "subject"=>"subject",
 "sender"=>"sender",
 "message"=>"message",
 "collector"=>"collector",
 "footer"=>"footer",
 "splat"=>["api/v2/emails"],
 "captures"=>["api/v2/emails"]}

问题是,POST请求仅使用URL和:body哈希。在:body哈希中,转到您要发送到服务器的所有变量。这就是代码的第二个版本,'collector''footer'有效。