Unexplained 401 error

I seem to have an unexplained problem trying to access data from api.transport.nsw.gov.au using Java.

I can issue an HTTP Get using POSTMAN to this endpoint (I’m seeking vehicle positions (busses)), and this is successfull. I get the payload. But in Java - I get an HTTP 401 error.

YES - I am putting in the same apikey :). and the same URL.

What am I missing ? Below is my small JAVA program:

package GTFSSample;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class Parse {
public static void main(String args) throws Exception {
// From URL
URL url = new URL(“https://api.transport.nsw.gov.au/v1/gtfs/vehiclepos/buses”);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setRequestProperty(“Authorization”, “apikey ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ”);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream());
String inputLine;
while ((inputLine = in.readLine()) != null)

thanks - Steve

Based on what you posted, it should work fine. Are you sitting behind a corporate firewall by any chance? Maybe that’s prompting the 401


As using postman, which works, is also behind corporate firewall, i didn’t think that was the problem. I agree it should work.

Perhaps an issue with https. I’ll do more research. I thought someone would already have a snippet already.



I have something similar, but:

  • I am adding gzip compression: connection.setRequestProperty(HttpHeaders.ACCEPT_ENCODING, "gzip");
  • I’m using addRequestProperty for the Authorization header.
  • I’m not setting the request method.

Yours definitely looks like it should work

© Transport for NSW