HTTP Header with API Key to access JSON

edited December 2017 in Library Questions

Hi all. I have been searching extensively over the last few days in a bid to find a way of access an online JSON API using an API key i have. So for a limited number of requests you do not need the API key, great for testing to start... i have managed to access the JSON using the LoadStrings method with no problem and now i have also managed to access it get data using the HTTP Requests For Processing Plugin (the updated one by Koogs: https://github.com/acdean/HTTP-Requests-for-Processing

My issue is now that i cant figure out how to add the header properly so that my request is accepted using the API Key... I have attached a screen shot of what i am trying so far, could a kind sole point me in the right direction?

Info on the API that i am trying to access is here: https://api.football-data.org/docs/v1/index.html

2017-12-23

P.S. The code will work at present as you get a set amount of free calls before having to use the API Key. I have locked it out previously though and cannot get the API key to be accepted in order to get more calls, hence my plea for help :-)

Thanks in advance for any help.

Answers

  • edited December 2017
    String teamChoice = "Tottenham Hotspur FC";
    
    import http.requests.*;
    
    public void setup() 
    {
      size(400,400);
      smooth();
    
      GetRequest get = new GetRequest("http://" + "api.football-data.org/v1/competitions/445/fixtures");
      get.send(); // program will wait untill the request is completed
      //println("response: " + get.getContent());
    
    
      //method: addHeader(name,value)
      get.addHeader("Accept", "application/json");
      get.addHeader("X-Application", "API Key xxxxx"); 
      //post.addHeader("Content-Type", "application/json");
    
      JSONObject response = parseJSONObject(get.getContent());
      println("status: " + response.getString("status"));
      JSONArray fixtures = response.getJSONArray("fixtures");
      println("fixtures: ");
    
     for (int a = 0; a < fixtures.size(); a++) 
     {
      String name = fixtures.getJSONObject(a).getString("awayTeamName");     
      if (name.equals(teamChoice) == true) 
      {
       String homeName = fixtures.getJSONObject(a).getString("homeTeamName");
       println("  HomeTeam:" + " " + homeName);     
      }   
     }
    }
    
  • Edit post, highlight code, press ctrl-o to format.

    Posting API keys is a terrible idea.

  • Thanks. Key is just a freebie so thought id keep it in to help, have taken it out now though :)

  • you are adding the headers (lines 16, 17) AFTER sending the request (line 11). you probably need to do it before.

  • Thanks. I had it that way before but it wasnt working, forgot to put it back to the logical way :(

    I was worried i wasnt adding the header in the correct way but it seems to be right from examples i have seen. I will keep on playing around with it.

  •   GetRequest get = new GetRequest("http://" + "api.football-data.org/v1/competitions/445/fixtures");
      get.addHeader("Accept", "application/json");
      get.send(); // program will wait untill the request is completed
    

    this seems to be enough, even without the api key. i get a list of fixtures, all of spurs' away games

    fixtures: 
      HomeTeam: Newcastle United FC
      HomeTeam: Everton FC
      HomeTeam: West Ham United FC
      HomeTeam: Huddersfield Town
      HomeTeam: Manchester United FC
      HomeTeam: Arsenal FC
      HomeTeam: Leicester City FC
      HomeTeam: Watford FC
      HomeTeam: Manchester City FC
      HomeTeam: Burnley FC
      HomeTeam: Swansea City FC
      HomeTeam: Southampton FC
      HomeTeam: Liverpool FC
      HomeTeam: Crystal Palace FC
      HomeTeam: AFC Bournemouth
      HomeTeam: Chelsea FC
      HomeTeam: Stoke City FC
      HomeTeam: Brighton & Hove Albion
      HomeTeam: West Bromwich Albion FC
    

    what are you expecting?

  • Thanks Koogs.

    I wanted to get the API key working as a personal test for me, to help understand and learn how it all works.

    As with most things i do its always the simplest thing that was wrong... X-Application should have simply been X-Auth-Token.... feeling a little silly now but glad i solved it. Now on to running the code a set number of times per hour :)

    Thanks for your responses, helped me keep digging :)

Sign In or Register to comment.