Created
October 3, 2013 20:50
-
-
Save jompa/6816901 to your computer and use it in GitHub Desktop.
Erlang httpc POST request to MailChimp API 2.0 in ChicagoBoss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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