Question

I received the following code from a development team:

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

I have been told that the above works fine. This is what I translated it to in Ruby 1.9.3, using the 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]"
)

Everything within this works fine except for the :footer and :collector parameters. It doesn't seem to recognize them at all.

There are no errors thrown, they just aren't included in the actual email I am sending. What am I doing wrong when passing in those two parameters?

Was it helpful?

Solution

Your :collector and :footer are not correct.

I wrote a little Sinatra service to receive a POST request with any parameters:

require 'pp'
require 'sinatra'

post "/*" do
  pp params
end

And ran it, launching the web-server on my Mac OS laptop. As Sinatra apps do, it resides at 0.0.0.0:4567.

Running this code:

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

Outputs:

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

Sinatra said:

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

Changing :collector and :footer to strings and moving them inside the body, where they should be:

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

Outputs:

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

And Sinatra said:

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

The problem is, the POST request ONLY uses a URL and a :body hash. Inside the :body hash go all the variables you're sending to the server. That's why the second version of the code, with 'collector' and 'footer' works.

OTHER TIPS

There is no comma after your :body parameter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top