Skip to content

Instantly share code, notes, and snippets.

@SidneyAllen
Created September 19, 2012 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SidneyAllen/3752855 to your computer and use it in GitHub Desktop.
Save SidneyAllen/3752855 to your computer and use it in GitHub Desktop.
SendGrid StackMob - Build URL and POST
//Encode any parameters that need encoding (i.e. subject, toname, text)
try {
subject = URLEncoder.encode(subject, "UTF-8");
text = URLEncoder.encode(text, "UTF-8");
toname = URLEncoder.encode(toname, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
}
String queryParams = "api_user=" + API_USER + "&api_key=" + API_KEY + "&to=" + to + "&toname=" + toname + "&subject=" + subject + "&text=" + text + "&from=" + from;
url = "https://www.sendgrid.com/api/mail.send.json?" + queryParams;
Header accept = new Header("Accept-Charset", "utf-8");
Header content = new Header("Content-Type", "application/x-www-form-urlencoded");
Set<Header> set = new HashSet();
set.add(accept);
set.add(content);
try {
HttpService http = serviceProvider.getHttpService();
PostRequest req = new PostRequest(url,set,body);
HttpResponse resp = http.post(req);
responseCode = resp.getCode();
responseBody = resp.getBody();
} catch(TimeoutException e) {
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment