Skip to content

Instantly share code, notes, and snippets.

@jacktasia
Created February 20, 2015 01:44
Show Gist options
  • Save jacktasia/1453fdaf470fe28494ff to your computer and use it in GitHub Desktop.
Save jacktasia/1453fdaf470fe28494ff to your computer and use it in GitHub Desktop.
imgix Java purge example (1.8 w/o dependencies)
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.net.ssl.HttpsURLConnection;
import java.net.URL;
import java.util.Base64;
import java.io.OutputStream;
public class ImgixPurgeExample {
// find your api key on the bottom of page @ https://webapp.imgix.com
private static final String API_KEY = "";
public static void purgeImage(String imgUrl) {
try {
URL url = new URL("https://api.imgix.com/v2/image/purger");
String authString = API_KEY + ":";
String encodedAuthString = Base64.getEncoder().encodeToString(authString.getBytes("utf-8"));
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + encodedAuthString);
connection.setRequestProperty("Content-Type", "application/json");
String str = "{\"url\":\"" + imgUrl + "\"}";
byte[] outputInBytes = str.getBytes("UTF-8");
OutputStream os = connection.getOutputStream();
os.write( outputInBytes );
os.close();
InputStream content = (InputStream)connection.getInputStream();
BufferedReader in = new BufferedReader (new InputStreamReader (content));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String urlToPurge = "http://yoursource.imgix.net/cat.png";
purgeImage(urlToPurge);
}
}
@manuelGitHub1
Copy link

Thank you for this code snippet. It saved me time and effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment