Created
February 20, 2015 01:44
-
-
Save jacktasia/1453fdaf470fe28494ff to your computer and use it in GitHub Desktop.
imgix Java purge example (1.8 w/o dependencies)
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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this code snippet. It saved me time and effort.