1

I'm trying to POST to Mailchimp in Ruby but I can't get any code working which has custom headers and a body. This is the request I am trying to replicate:

curl --request GET \
--url 'https://<dc>.api.mailchimp.com/3.0/' \
--user 'anystring:<your_apikey>'

but I also have to add a JSON body.

If I run this code:

postData = Net::HTTP.post_form(URI.parse('https://xxx.api.mailchimp.com/3.0/lists/xxx/members/'), { ... }})
puts postData.body

I get a response from Mailchimp that the apikey is missing. How do I add the API key?

Based on these posts: Ruby request to https - "in `read_nonblock': Connection reset by peer (Errno::ECONNRESET)"

Ruby send JSON request

I tried this:

uri = URI('https://xxx.api.mailchimp.com/3.0/lists/xxxx/members/')
req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
req.basic_auth 'anystring', 'xxxx'
req.body = URI.encode_www_form({ ... }})
response = Net::HTTP.new(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https').start {|http| http.request(req) }
puts "Response #{response.code} #{response.message}:#{response.body}"

but I get the error TypeError (no implicit of Hash into String) on the response = ... line. What is the error referring to and how do I fix it?

UPDATE:

using start instead of new:

response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http| http.request(req) }

I am able to send the request, but I get a 400 response: "We encountered an unspecified JSON parsing error"

I get the same response with the posted answer. here is my JSON:

{'email_address' => 'xxxx@gmail.com', 'status' => 'subscribed', 'merge_fields' => {'FNAME' => 'xxx', 'LNAME' => 'xxx' }

I also tried adding the data like this:

req.set_form_data('email_address' => 'xxxx@gmail.com', 'status' => 'subscribed', 'merge_fields' => {'FNAME' => 'xxx', 'LNAME' => 'xxx' } )

but I get the same JSON parse error

Community
  • 1
  • 1
Cbas
  • 6,003
  • 11
  • 56
  • 87
  • did you try the `mainchimp ruby library`. https://bitbucket.org/mailchimp/mailchimp-api-ruby – Shiva May 12 '16 at 02:06
  • hm I'll try it but its only for v2 API, and I'd rather use v3 – Cbas May 12 '16 at 02:09
  • then use this https://github.com/mailchimp/APIv3-examples/tree/master/ruby – Shiva May 12 '16 at 02:18
  • 1
    The reason you're getting a TypeError is that [`Net::HTTP.new` doesn't take a hash argument](http://ruby-doc.org/stdlib-2.3.1/libdoc/net/http/rdoc/Net/HTTP.html#method-c-new). – Jordan Running May 12 '16 at 02:20
  • @Jordan thanks, I tried using start instead of new and it sent the request but now I get a JSON parsing error. I updated with the code – Cbas May 12 '16 at 02:38
  • Try `req.body = {...}.to_json`? – Amadan May 12 '16 at 02:42
  • @Amadan yea that was it, going to accept the other answer though b/c it also cleans the code up a lot – Cbas May 12 '16 at 03:40

1 Answers1

1

Try this, if it works for you

uri = URI('https://api.mailchimp.com/3.0/lists/xxxx/members/')

Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  req = Net::HTTP::Post.new(uri)
  req['Content-Type'] = 'application/json'
  req.basic_auth 'username', 'password'
  req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')

  response = http.request req # Net::HTTPResponse object
end

You need to set form data in post request like

req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')

Updates:

Try posting raw data like

json_data = {'from' => '2005-01-01', 'to' => '2005-03-31'}.to_json
req.body = json_data
Shiva
  • 11,485
  • 2
  • 67
  • 84
  • Oh may be the data-submitted is not valid, are you willing to post form data or raw_data? – Shiva May 12 '16 at 02:59
  • Oh actually there is a 400 error if I print out the results, its a JSON parse error. I added details to the question – Cbas May 12 '16 at 03:05
  • i have updated the answer, please verify if you have tried to post formdata the correct way – Shiva May 12 '16 at 03:09
  • Ok I tried but got the same JSON parse error. I added the code to the question – Cbas May 12 '16 at 03:17
  • is `mailchip` returning this error? please see the api documentation; try posting the raw_data.. see the updates – Shiva May 12 '16 at 03:23
  • http://stackoverflow.com/questions/29371622/posting-ruby-data-in-json-format-with-net-http – Shiva May 12 '16 at 03:25
  • 1
    yea the raw data did it. I am still getting an error but its asking for a field that isn't in the API so I can talk to mailchimp support to sort it out – Cbas May 12 '16 at 03:42
  • [I was able to get it working - looks like Mailchimp allows you to add required custom fields that you can only see in the dashboard] – Cbas May 12 '16 at 03:56