Problem to get data using API


#1

Hi everyone,
I have problem when try to request data with API key. I am using html and Jquery + ajax, my code is as following

$(document).ready(function () {

          $('#datadisplay').ready(function () {
              var displayResources = $('#datadisplay');

              displayResources.text('Loading data from JSON source...');

              $.ajax({
                  type: "GET",
                  url: "https://api.transport.nsw.gov.au/v1/gtfs/schedule/nswtrains",
                  headers: {
                      
                       'api-key' : 'apikeyxxxxxxxxxxxxx',
                        userId : 'uname',
                        password : 'pass'
                  },

                  success: function (result) {

                      alert("execute");
                     
                  }
              });

          });
      });

I got response as 500
pleas any one help me.


#2

Hi @Hardik, the API seems to be working fine, just tried it using our API explorer.

Can you please try again after adding a space between “apikey” and “xxxxxxxxxxxxxx”. Make sure the headers are in the correct format and syntax.

Thanks,
Alex


#3

Thanks for Reply Alejandro
I am using nswtrain data file which compressed in 6 text file in a single zip file. and i would like to access those file using html, ajax, jQuery and display on html elements. is that possible??
.
thanks


#4

That is definitely possible, we don’t provide programming advice or guidance though.

Best place to start would be to use our API explorer to understand how our APIs work. Also recommend you read this blog post https://opendata.transport.nsw.gov.au/how-use-open-data-develop-application and our get started guide https://opendata.transport.nsw.gov.au/get-started

Once you are able to call our APIs programatically you will then be able to develop an app or website that uses and displays our data.

If you have any more specific coding questions I’m sure there are some friendly developers on the forum that can help you out.

Thanks,
Alex


#5

Is there any error message with the 500? Generally 500 indicates an issue on the server (TfNSW)'s side.

I would expect your headers should look like: Authorization: "apikey xyz". No username, no password.


#6

Thanks Jayen for replying
I have resolved my problem … look at this code for jQuery API calling
parsing zip. and getting text file

async function req() {
var request = new XMLHttpRequest();
request.responseType = “blob”;
//request.onload = handleFile;
await request.open(“GET”, “https://api.transport.nsw.gov.au/v1/gtfs/schedule/nswtrains”);
var headers = { “Authorization”: “apikey GhSIxxxxxxxxxxx” };
for (var prop in headers) {
request.setRequestHeader(prop, headers[prop]);
}
// request.setRequestHeader(‘Content-type’, ‘application/json; charset=utf-8’);
//let retdata = await request.send();
await request.send();

          request.onload = function (e) {
                        //  preloader.modal('hide');
                          if (this.status == 200) {
                              var rtype = request.getResponseHeader('Content-Type');
                              var blob = new Blob([this.response], { type: rtype });
                              var downloadUrl = URL.createObjectURL(blob);
                              var a = document.createElement("a");
                              a.href = downloadUrl;
                              a.download = "new.zip";
                              document.body.appendChild(a);
                             // a.click();

                              var promise = new JSZip.external.Promise(function (resolve, reject) {
                                  JSZipUtils.getBinaryContent(downloadUrl, function (err, data) {
                                      if (err) {
                                          reject(err);
                                      } else {
                                          resolve(data);
                                      }
                                  });
                              });

                              promise.then(JSZip.loadAsync)                     // 2) chain with the zip promise
                                  .then(function (zip) {
                                      return zip.file("routes.txt").async("string"); // 3) chain with the text content promise
                                  })
                                  .then(function success(text) {
                                
                                      var employee_data = text.split(/\r?\n|\r/);
                                      var table_data = '<table class="table table-bordered table-striped">';
                                      for (var count = 0; count < employee_data.length; count++) {
                                          var cell_data = employee_data[count].split(",");
                                          table_data += '<tr>';
                                          for (var cell_count = 0; cell_count < cell_data.length; cell_count++) {
                                              if (count === 0) {
                                                  table_data += '<th>' + cell_data[cell_count].replace(/['"]+/g, '') + '</th>';
                                              }
                                              else {
                                                  table_data += '<td>' + cell_data[cell_count].replace(/['"]+/g, '') + '</td>';
                                              }
                                          }
                                          table_data += '</tr>';
                                      }
                                      table_data += '</table>';
                                      $('#employee_table').html(table_data);
                      
                                       $("#jszip_utils").append($("<p>", {
                                          text: "loaded, content = " + text
                                      }));
                                  }, function error(e) {
                                      $("#jszip_utils").append($("<p>", {
                                          "class": "alert alert-danger",
                                          text: e
                                      }));
                                  });

                          } else {
                              alert('Unable to download excel.')
                          }
          };