API getting .xml via URL (dutch railways)

edited July 2014 in How To...

Hi,

I'm experimenting a bit with using the API of the dutch railways. It's a open API and available from: http://www.ns.nl/api/api. Unfortunately everything is in dutch, but luckily that's irrelevant for my question.

I'm trying to get the data into processing. You can request the info via simple URL's. For instance:

http://webservices.ns.nl/ns-api-treinplanner?fromStation=Utrecht+Centraal&toStation=Wierden&departure=true

it requires a login that needs your email and a key. Which you can include in the URL for automatic login. This an example which is working ( I made a dummy one on a email adress I use for testing etc)

login: ruben_vleuten@hotmail.com key: RghFLhdU_ot9g8tcVvlnjJa_zcToVTM0H1iHB-XchgprrgrZk3SJhA

http://ruben_vleuten@hotmail.com:RghFLhdU_ot9g8tcVvlnjJa_zcToVTM0H1iHB-XchgprrgrZk3SJhA@webservices.ns.nl/ns-api-treinplanner?fromStation=Utrecht+Centraal&toStation=Wierden&departure=true

When I use this URL in my browser it returns xml so it works properly. But the problems is when I try to import it in a processing sketch. When I do something like this it gives me an error.

XML testXml;

void setup() { testXml = loadXML("http://ruben_vleuten@hotmail.com:RghFLhdU_ot9g8tcVvlnjJa_zcToVTM0H1iHB-XchgprrgrZk3SJhA@webservices.ns.nl/ns-api-treinplanner?fromStation=Utrecht+Centraal&toStation=Wierden&departure=true");

println(testXml);

}

It seems to work with other URL's so don't really know what the problem is. I'm also not very experienced using API's so it might be a rookie mistake.

Hope someone can help.

Cheers,

Ruben.

Tagged:

Answers

  • (i've edited your post to stop the forum treating the url in the code as special)

  • Quick answer (lack of time): you can try and use the Client class (from the Network library) to access the data. One advantage is that it allows to specify some headers, like giving a fake User-Agent header. Some APIs try to do automatic detection of client, and might require some data to work properly.
    There are several threads around similar issues.
    I will come back to this issue later, to do more experiments.

    The part with the login can be disturbing too. You should look how it is done in regular Java, perhaps.

  • Answer ✓

    OK, I had time tonight, and was successful in getting data from the site:

    import processing.net.*;
    import javax.xml.bind.DatatypeConverter;
    
    String encodedAuth = DatatypeConverter.printBase64Binary("ruben_vleuten@hotmail.com:RghFLhdU_ot9g8tcVvlnjJa_zcToVTM0H1iHB-XchgprrgrZk3SJhA".getBytes());
    Client client;
    int step = 0;
    
    void setup()
    {
      size(200, 200);
      frameRate(1);
      step = 1;
      println(encodedAuth);
    }
    
    void draw()
    {
      if (step > 0)
      {
        println("------ " + step);
        client = new Client(this, "webservices.ns.nl", 80);
      }
      switch (step)
      {
      case 1:
        client.write("GET /ns-api-treinplanner?fromStation=Utrecht+Centraal&toStation=Wierden&departure=true HTTP/1.1\r\n");
        client.write("Host: webservices.ns.nl\r\n");
        client.write("Authorization: Basic " + encodedAuth);
        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");
        break;
      case 2:
        exit();
      }
      if (step > 0)
      {
        step = -++step;
      }
      if (client.available() > 0)
      {
        String dataIn = client.readString();
        println(dataIn);
        step = -step;
      }
    }
    
  • Hi PhiLho

    This is great. Thank you so much. Will need some thing to figure out what is exactly happening. But seems like it's working. So thank you a lot, this is more than I asked for :).

Sign In or Register to comment.