Working with Client connection to API with authentication

edited April 2015 in Library Questions

I am trying to use Client for the first time and having issues connecting to a specific API (api.imagga.com).

I am trying to reproduce the sample code from their docs (http://docs.imagga.com/index.html) that looks like this and provides a response:

curl -H "Authorization: Basic YWNjXzYzY2Q2NzQ3YmRjMDgzYzo5N2Q5OTBmNzllNDkzYjUwOGJlODdjYWRmODRlNmFhNg==" "http://api.imagga.com/v1/tagging?url=http://playground.imagga.com/static/img/example_photos/japan-605234_1280.jpg"

I am hitting a wall when I start making requests as the client is simply returning "null" and not providing any errors to work with. I am hoping that someone with more experience working with servers and APIs in Processing can help me get this up and running. Here's what I've got:

import processing.net.*;

String encodedAuth = "YWNjXzYzY2Q2NzQ3YmRjMDgzYzo5N2Q5OTBmNzllNDkzYjUwOGJlODdjYWRmODRlNmFhNg==";
Client client;

void setup() {
  client = new Client(this, "api.imagga.com", 80);
  client.write("GET HTTP/1.0 \r\n");
  client.write("authorization: Basic " + encodedAuth);
  client.write("host: api.imagga.com\r\n");
  client.write("/v1/tagging?url=http%3A%2F%2Fplayground.imagga.com%2Fstatic%2Fimg%2Fexample_photo.jpg\r\n");
  client.write("\r\n"); // indicates end of request?

  String dataIn = client.readString();
  println(dataIn);
}

void draw() {
}
Tagged:

Answers

  • You miss a \r\n at the end of the authorization line.

  • edited April 2015

    Thanks @PhiLho. That was certainly an issue, and has helped move me closer to getting this working, but I'm still in the weeds. It also points to one of the problems I'm having with the Client object… I can't see what is happening. How can I get more output to the console to ensure that I'm sending rational commands to the server?

    I'm at the point where I'm receiving "HTTP/1.1 400 BAD_REQUEST", but I still feel as if I'm operating blind.

    import processing.net.*;
    
    String encodedAuth = "YWNjXzYzY2Q2NzQ3YmRjMDgzYzo5N2Q5OTBmNzllNDkzYjUwOGJlODdjYWRmODRlNmFhNg==";
    Client client;
    String dataIn;
    
    void setup() {
      client = new Client(this, "api.imagga.com", 80); 
      client.write("GET HTTP/1.0 \r\n");
      client.write("authorization: Basic " + encodedAuth + "\r\n");
      client.write("host: api.imagga.com\r\n");
      client.write("/v1/tagging?url=http%3A%2F%2Fplayground.imagga.com%2Fstatic%2Fimg%2Fexample_photo.jpg\r\n");
      client.write("\r\n");
    }
    
    void draw() {
      if (client.available() > 0) {
        dataIn = client.readString();
        println(dataIn);
      }
    }
    
  • edited April 2015 Answer ✓

    Please replace encodedAuth with your own encoded string.


    import processing.net.*;
    
    String encodedAuth = "";
    
    Client client;
    String data;
    
    void setup() {
      client = new Client(this, "api.imagga.com", 80);
      client.write("GET /v1/tagging?url=http%3A%2F%2Fplayground.imagga.com%2Fstatic%2Fimg%2Fexample_photo.jpg HTTP/1.0\r\n");
      client.write("Host: api.imagga.com\r\n");
      client.write("Authorization: Basic " + encodedAuth + "\r\n");
      client.write("Accept: application/xml\r\n");
      client.write("Accept-Charset: utf-8;q=0.7,*;q=0.7\r\n");
      client.write("\r\n");
    }
    
    void draw() {
      if (client.available() > 0) {    // If there's incoming data from the client...
        data += client.readString();   // ...then grab it and print it 
        println(data);
      }
    }
    
  • edited April 2015

    I'm getting a 401 unauthorized using what's below. Maybe just need a new bearer token?

    import processing.net.*;
    
    String encodedAuth = "YWNjXzYzY2Q2NzQ3YmRjMDgzYzo5N2Q5OTBmNzllNDkzYjUwOGJlODdjYWRmODRlNmFhNg==";
    Client client;
    String dataIn;
    
    void setup() {
      client = new Client(this, "api.imagga.com", 80); 
      client.write("GET /v1/tagging?url=http%3A%2F%2Fplayground.imagga.com%2Fstatic%2Fimg%2Fexample_photo.jpg HTTP/1.0 \r\n");
      client.write("host: api.imagga.com\r\n");
      client.write("authorization: Basic " + encodedAuth + "\r\n");
    //  client.write("/v1/tagging?url=http%3A%2F%2Fplayground.imagga.com%2Fstatic%2Fimg%2Fexample_photo.jpg\r\n");
      client.write("\r\n");
    }
    
    void draw() {
      if (client.available() > 0) {
        dataIn = client.readString();
        println(dataIn);
      }
    }
    
Sign In or Register to comment.