Skip to content

Instantly share code, notes, and snippets.

@shubs
Last active August 29, 2015 14:21
Show Gist options
  • Save shubs/4a44f5b0c3d70358140c to your computer and use it in GitHub Desktop.
Save shubs/4a44f5b0c3d70358140c to your computer and use it in GitHub Desktop.
Code for a standalone usage of the Mailjet API in JAVA

How to use it

  • In your root directory, copy the Makefile
  • Create a src folder and copy the file HttpURLConnectionMailjet.java into it
  • You will need the commons-codec.jar file. Download it from http://commons.apache.org/proper/commons-codec/download_codec.cgi.
  • Create a lib folder and download the file commons-codec.jar into this folder
  • Fill your Mailjet API Keys (line 20-21) in the file HttpURLConnectionMailjet.java
  • Then just performe a make
/**
* Code for a standalone usage of the mailjet API
*/
//some import
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
import javax.net.ssl.HttpsURLConnection;
//class definition
public class HttpURLConnectionMailjet {
private final String USER_AGENT = "Mozilla/5.0";
private final String MJ_APIKEY_PUBLIC = "FIXME";
private final String MJ_APIKEY_PRIVATE = "FIXME";
public static void main(String[] args) throws Exception {
HttpURLConnectionMailjet http = new HttpURLConnectionMailjet();
System.out.println("Testing 1 - Send Http GET request");
http.sendGet();
// System.out.println("\nTesting 2 - Send Http POST request");
// http.sendPost();
}
// HTTP GET request
private void sendGet() throws Exception {
String url = "https://api.mailjet.com/v3/REST/listrecipient";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
String authString = MJ_APIKEY_PUBLIC + ":" + MJ_APIKEY_PRIVATE;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
con.setRequestProperty("Authorization", "Basic " + authStringEnc);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
// HTTP POST request
private void sendPost() throws Exception {
String url = "https://api.mailjet.com/v3/REST/contact";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String authString = MJ_APIKEY_PUBLIC + ":" + MJ_APIKEY_PRIVATE;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
String urlParameters = "Param1=C02G8416DRJM&Param2=&locale=&Param3=&param4=12345";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
all:
javac -cp lib/commons-codec-1.10.jar src/HttpURLConnectionMailjet.java
cd src && java -cp ../lib/commons-codec-1.10.jar:. HttpURLConnectionMailjet
clean:
rm src/HttpURLConnectionMailjet.class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment