Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

manuelGitHub1 commented Dec 23, 2019

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