Im mucking around with an Angular 2 app that consumes parts of this API. Im able to do gets in postman without any issue, but when i try to do it from my app i get this error in the console
XMLHttpRequest cannot load https://api.transport.nsw.gov.au/v1/gtfs/schedule/buses/Major_Event. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.
I was hoping someone else can respond but I have a vague recollection that this is because you havenât used the proper credentials or set up the details correctly.
Check you have the correct setup in the Application side of things in the API Gateway including following the details on this page:
Iâm fairly sure that the API does not support cross-domain/cross-origin requests. For security reasons, browsers do not allow requests XMLHttpRequests to be made between sites of different domain by default.
Your originating domain is localhost:4200 whereas the server youâre requesting is api.transport.nsw.gov.au. Since the domains arenât matching, and your domain (localhost:4200) isnât listed in TfNSWâs Access-Control-Allow-Origin header response, youâre presented with an error.
The reason why Postman works is that as a browser extension, youâve granted the extension âpermissionâ to make requests to other servers with non-matching domains.
I should also add that the GTFS-R data isnât meant to be consumed directly by an end user client. Rather, it should be consumed by a server-side application, digested/processed, then fed to a front-end client (e.g. a web based client built in Angular).
You could build something on your server side which downloads the GTFS-R data from TfNSWâs servers, processes the protocol buffers and returns the data in a JSON format. That would easily be digestable by a modern browser and in your Angular application.
Hope that helps.
Ken
edit: I guess you could also make your Angular application a browser extension
I ended up making a small .net app to do the web request and spit out a .zip for me to use
If anyone else comes across this in the future, there is apparently a chrome extension that you can get that will turn off the CORS security, but i dont think thats a great solution to anything.