Hardik
October 8, 2018, 9:38am
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.
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
1 Like
Hardik
October 9, 2018, 12:31am
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
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 How to Use Open Data to Develop an Application | TfNSW Open Data Hub and Developer Portal and our get started guide Get Started | TfNSW Open Data Hub and Developer Portal
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
jayen
October 11, 2018, 8:55am
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.
Hardik
October 18, 2018, 11:14pm
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.')
}
};