0

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Luigi
  • 5,443
  • 15
  • 54
  • 108
  • There is no comma after your :body parameter – Raghu Sep 11 '13 at 23:17
  • My apologies - I just left the comma off, it is indeed there in the actual code snippet. I have edited the post to reflect this. – Luigi Sep 11 '13 at 23:21
  • The collector value should be "COLLECTOR" not collector . it should be a string – Raghu Sep 11 '13 at 23:22
  • can you tell me from where you are getting the value for basic_auth ? The value auth dont seem to defined anywhere – Raghu Sep 11 '13 at 23:23
  • `auth` is defined previously, I just included the code in question. `auth` is working fine. The API call is succeeding, just the two parameters mentioned are not. `COLLECTOR` is a variable defined by the development team, it is equal to `collector` in my code. – Luigi Sep 11 '13 at 23:33
  • Can you paste the entire code? it's hard to debug without knowing how the variables are being defined – Raghu Sep 11 '13 at 23:35
  • We can't really help you unless you provide some usable code. Looking at what you've written doesn't help nearly as much as code that can be run, but, unfortunately, yours is missing the definitions of variables. "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See [SSCCE.org](http://sscce.org/) for guidance." – the Tin Man Sep 11 '13 at 23:56

2 Answers2

0

There is no comma after your :body parameter

Raghu
  • 2,543
  • 1
  • 19
  • 24
0

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Incredible. Thanks for the help, that's exactly what I was looking for! – Luigi Sep 12 '13 at 12:59
  • You're new here. If it's exactly what you're looking for, then you should select it as the accepted answer. That's what proper procedure is on Stack Overflow. – the Tin Man Sep 12 '13 at 14:54