Can't open an url with loadJSONObject but it works with HTTP Library

Hello! I got a problem that I can’t understand. I need to load this url into a Processing sketch: https://www.colourlovers.com/api/palettes/top?format=json
I used loadJSONArray without any problem in the past but now I got this error:

java.io.IOException: Server returned HTTP response code: 403 for URL: …

If I open the same url using the HTTP Library it works without any problem:

JSONArray second;
GetRequest get = new GetRequest("https://www.colourlovers.com/api/palettes/top?format=json");
get.send();
second = parseJSONArray(get.getContent());

Any suggestion on how to solve it using loadJSONArray?

Thanks!

1 Like

I’m sure that the problem can be solved by adding a user agent to the request. Is it possible to do so with the function loadjson? I looked into the javadoc but couldn’t find any hint

I solved the problem using java’s HTTPUrlConnection to add the User Agent to my request and then by parsing the output to JSON

Demo based on prev post.

Kf

//REFERENCES: https://forum.processing.org/two/discussion/5782/loadstrings-url-403-error


//===========================================================================
// IMPORTS:
import java.net.*;
import java.io.*;


//===========================================================================
// PROCESSING DEFAULT FUNCTIONS:

void settings(){
  size(400,600);
}

void setup(){

  textAlign(CENTER,CENTER);
  rectMode(CENTER);
  
  fill(255);
  strokeWeight(2);
  noLoop();
}

void draw(){
  background(0);
  String txt=getData("https://www.colourlovers.com/api/palettes/top?format=json"); /  
  JSONArray json = parseJSONArray(txt);  
  println(txt);
  
}

String getData(String urlsite){
  
  String htmlText="";;

  try {
    // Create a URL object
    URL url = new URL(urlsite);
    URLConnection conn = url.openConnection();
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
    // Read all of the text returned by the HTTP server
    BufferedReader in = new BufferedReader
      (new InputStreamReader(conn.getInputStream(), "UTF-8"));
 
    String readLine;
 
    while ( (readLine = in.readLine ()) != null) {
      // Keep in mind that readLine() strips the newline characters
      System.out.println(readLine);
      htmlText = htmlText+'\n'+readLine;
    }
    in.close();
  } 
  catch (MalformedURLException e) {
    e.printStackTrace();
  } 
  catch (IOException e) {
    e.printStackTrace();
  }
  
  return htmlText;
}

1 Like

This is what I did, I just used a Collector to deal with the BufferedReader’s output.

import java.util.stream.Collectors;

BufferedReader ir = new BufferedReader(new InputStreamReader((InputStream) connection.getContent()));
return parseJSONArray(ir.lines().collect(Collectors.joining()));
1 Like