# Using Headers for loading Images

**URL:** <https://discourse.processing.org/t/using-headers-for-loading-images/31450>\
**Category:** Coding Questions\
**Created:** [July 26, 2021, 5:31pm UTC](https://discourse.processing.org/t/using-headers-for-loading-images/31450 "2021-07-26T17:31:59Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![Flolo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/flolo/32/13874_2.png) [@Flolo](https://discourse.processing.org/u/Flolo)\
**Post date:** [July 27, 2021, 1:23pm UTC](https://discourse.processing.org/t/using-headers-for-loading-images/31450/7 "2021-07-27T13:23:56Z")

</div>

Hey @Glenton I dont know if this helps:  
Its a little class which downloads an Image and then loads it via `loadImage()` from processing

> **Click to see the my Example**
>
> ```auto
> String URL = "https://img.discogs.com/UoDAnoi3Cbp_JvOrz4CRgZBBSfg=/fit-in/600x592/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/R-367084-1263095553.jpeg.jpg";
> PImage img;
> void setup() {
> size(100, 100);
> //img = requestImage(URL);//not working
> //img = loadImage(URL);//not working
> 
> DownloadImage downImg = new DownloadImage(this);
> img = downImg.load(URL, sketchPath("test.png"));
> 
> if (img == null) {
> println("Error");
> return;
> }
> println("Image loaded");
> surface.setSize(img.width, img.height);
> }
> void draw() {
> background(0);
> 
> if (img == null) return;
> image(img, 0, 0);
> }
> 
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.OutputStream;
> import java.net.URL;
> import java.net.URLConnection;
> 
> public class DownloadImage {
> PApplet parent;
> 
> public DownloadImage(PApplet parent) {
> this.parent = parent;
> }
> public PImage load(String search, String path) {
> 
> // This will get input data from the server
> InputStream inputStream = null;
> 
> // This will read the data from the server;
> OutputStream outputStream = null;
> 
> try {
> // This will open a socket from client to server
> URL url = new URL(search);
> 
> // This user agent is for if the server wants real humans to visit
> String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
> 
> // This socket type will allow to set user_agent
> URLConnection con = url.openConnection();
> 
> // Setting the user agent
> con.setRequestProperty("User-Agent", USER_AGENT);
> 
> // Requesting input data from server
> inputStream = con.getInputStream();
> 
> // Open local file writer
> outputStream = new FileOutputStream(path);
> 
> // Limiting byte written to file per loop
> byte[] buffer = new byte[2048];
> 
> // Increments file size
> int length;
> 
> // Looping until server finishes
> while ((length = inputStream.read(buffer)) != -1) {
> // Writing data
> outputStream.write(buffer, 0, length);
> }
> } 
> catch (Exception ex) {
> ex.printStackTrace();
> }
> 
> // closing used resources
> // The computer will not be able to use the image
> // This is a must
> 
> try {
> outputStream.close();
> inputStream.close();
> } 
> catch (IOException e) {
> e.printStackTrace();
> }
> 
> PImage img = parent.loadImage(path);
> return img;
> }
> }
> 
> ```

> **Click to see the Example in your code**
>
> ```auto
> String searchString;
> JSONObject json;
> 
> PImage cover;
> 
> void setup()
> {
> searchString = ("https://api.discogs.com/database/search?q=Nirvana%20nevermind");
> testdiscogs();
> }
> 
> void draw() {
> if (cover == null) return;
> image(cover, 0, 0);
> }
> 
> void testdiscogs() {
> JSONObject json;
> int masterID = 0;
> 
> println("test discogs");
> try {
> println(searchString);
> GetRequest get = new GetRequest(searchString);
> get.addHeader("Authorization", "Discogs key="+discogsConsumerKey+", secret="+discogsConsumerSecret);
> get.addHeader("user-agent", "omnijukebot/0.0 bcrepet@yahoo.fr");
> get.send();
> json = parseJSONObject(get.getContent());
> JSONArray results = json.getJSONArray("results");
> if (results.size()>0) {
> println("Au moins un master trouvé");
> masterID = results.getJSONObject(0).getInt("master_id");
> println("master_id:" + masterID);
> DownloadImage downImg = new DownloadImage(this);
> String url = results.getJSONObject(0).getString("cover_image");
> cover = downImg.load(url, sketchPath("test.png"));
> }
> }
> catch (Exception e) {
> println("probleme test discogs e="+e);
> }
> if (cover == null) return;
> surface.setSize(cover.width, cover.height);
> }
> 
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.OutputStream;
> import java.net.URL;
> import java.net.URLConnection;
> 
> public class DownloadImage {
> PApplet parent;
> 
> public DownloadImage(PApplet parent) {
> this.parent = parent;
> }
> public PImage load(String search, String path) {
> 
> // This will get input data from the server
> InputStream inputStream = null;
> 
> // This will read the data from the server;
> OutputStream outputStream = null;
> 
> try {
> // This will open a socket from client to server
> URL url = new URL(search);
> 
> // This user agent is for if the server wants real humans to visit
> String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
> 
> // This socket type will allow to set user_agent
> URLConnection con = url.openConnection();
> 
> // Setting the user agent
> con.setRequestProperty("User-Agent", USER_AGENT);
> 
> // Requesting input data from server
> inputStream = con.getInputStream();
> 
> // Open local file writer
> outputStream = new FileOutputStream(path);
> 
> // Limiting byte written to file per loop
> byte[] buffer = new byte[2048];
> 
> // Increments file size
> int length;
> 
> // Looping until server finishes
> while ((length = inputStream.read(buffer)) != -1) {
> // Writing data
> outputStream.write(buffer, 0, length);
> }
> } 
> catch (Exception ex) {
> ex.printStackTrace();
> }
> 
> // closing used resources
> // The computer will not be able to use the image
> // This is a must
> 
> try {
> outputStream.close();
> inputStream.close();
> } 
> catch (IOException e) {
> e.printStackTrace();
> }
> 
> PImage img = parent.loadImage(path);
> return img;
> }
> }
> 
> ```

---

_[View the full topic](https://discourse.processing.org/t/using-headers-for-loading-images/31450)._
