【问题标题】:How can I get HTTParty to recognize ":footer" and ":collector" parameters?如何让 HTTParty 识别 ":footer" 和 ":collector" 参数?
【发布时间】:2013-09-11 23:14:08
【问题描述】:

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

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 参数外,其中的所有内容都可以正常工作。它似乎根本不认识他们。

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

【问题讨论】:

  • 你的 :body 参数后面没有逗号
  • 我很抱歉 - 我只是把逗号去掉了,它确实存在于实际代码 sn-p 中。我已经编辑了帖子以反映这一点。
  • 收集器值应该是 "COLLECTOR" 而不是收集器。它应该是一个字符串
  • 你能告诉我你从哪里得到 basic_auth 的价值吗?值 auth 似乎没有在任何地方定义
  • auth 是之前定义的,我只是包含了有问题的代码。 auth 工作正常。 API调用成功,只是提到的两个参数没有。 COLLECTOR 是开发团队定义的变量,在我的代码中等于collector

标签: ruby curl httparty


【解决方案1】:

您的: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 - - [2013 年 9 月 11 日 17:58:47] “POST /api/v2/emails?survey=1 HTTP/1.1”200 - 0.0163 {"调查"=>"1", “主题”=>“主题”, “发件人”=>“发件人”, “消息”=>“消息”, "splat"=>["api/v2/emails"], “捕获”=>[“api/v2/电子邮件”]}

: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"]]

辛纳屈说:

127.0.0.1 - - [2013 年 9 月 11 日 18:04:13] “POST /api/v2/emails?survey=1 HTTP/1.1”200 - 0.0010 {"调查"=>"1", “主题”=>“主题”, “发件人”=>“发件人”, “消息”=>“消息”, “收藏家”=>“收藏家”, “页脚”=>“页脚”, "splat"=>["api/v2/emails"], “捕获”=>[“api/v2/电子邮件”]}

问题是,POST 请求仅使用 URL 和 :body 哈希。在:body 散列中包含您发送到服务器的所有变量。这就是代码的第二个版本,'collector''footer' 起作用的原因。

【讨论】:

  • 难以置信。感谢您的帮助,这正是我想要的!
  • 你是新来的。如果这正是您正在寻找的,那么您应该选择它作为接受的答案。这就是 Stack Overflow 上的正确程序。
【解决方案2】:

:body 参数后面没有逗号

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2016-09-20
    相关资源
    最近更新 更多