Skip to content

Instantly share code, notes, and snippets.

@jompa
Created October 3, 2013 20:50
Show Gist options
  • Save jompa/6816901 to your computer and use it in GitHub Desktop.
Save jompa/6816901 to your computer and use it in GitHub Desktop.
Erlang httpc POST request to MailChimp API 2.0 in ChicagoBoss
-module(myapp_register_controller, [Req]).
-compile(export_all).
-define(API_KEY, "mykey").
-define(LIST_ID, "mylist").
add('GET', [])->
ok;
add('POST', [])->
Email = Req:post_param("email"),
{ok, {{Version, Status, ReasonPhrase}, Headers, Body}} = post_email_to_mailchimp(Email),
case Status of
200 ->
Message = "All's well";
500 ->
Message = ReasonPhrase
end,
{json, [{status, Status}, {message, Message}]}.
post_email_to_mailchimp(Email)->
inets:start(),
ssl:start(),
Data = "apikey=" ++ ?API_KEY ++ "&id=" ++ ?LIST_ID ++ "&email[email]=" ++ Email,
Request = httpc:request(post, {
"https://us6.api.mailchimp.com/2.0/lists/subscribe",
[],
"application/x-www-form-urlencoded",
Data
},
[], []),
Request.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment