Hello,
I am trying to access the API from localhost using an Angular app. At this moment, I constantly have to upload the app to a running server to circumvent this block. However, this is very developer unfriendly.
I have been reading about many similar requests and various workarounds were proposed. However, Angular does not allow me to set a different origin, resulting in that these proposed solutions do not work for Angular applications.
@MArkWilliams, can you please allow requests from localhost?
Thank you.
Best, Tim
I agree - this would clearly be a popular and helpful change for Companies House to make.
However - using the workaround you’ve read about - e.g. registering a dummy host name with Companies House (in the authorised Domains section) and then adding name in the hosts file - should work for you. It seems to do for others and some are using Angular also. I agree that this not particularly obvious without this being included in the documentation, but there are extensive resources in this forum about the issue.
If you’re still stuck after checking the common issues (including ones not directly related to CORS e.g. are you sending the correct API key / encoding it correctly etc.) you could try posting some sample code here - Angular experts may be able to advise - I’m not one.
You said:
Angular does not allow me to set a different origin
The “Origin” header has nothing to do with Angular or other language per se - this should be done by the browser in this case (see e.g. this document about this error in Angular). (Caveat - I’m not an Angular user but do use CORS. I have had a quick look at the httpClient documentation and there doesn’t seem to be anything extra that is required for CORS in Angular. I may have missed it! For example in jQuery you need to specify “crossdomain: true” in the parameters for CORS to work).
For CORS to work, Companies House servers have to return an “Access-Control-Allow-Origin” header - and they’ll only permit the hosts registered with them. This is where the dummy host mapped to “localhost” workaround comes in. If the “Origin” header is not being sent that suggests that whatever is being sent doesn’t match what’s registered at the Companies House end.
Previous posts cover some debugging of this issue e.g. my post on another thread. Some of the possible problems are discussed - from an Angular perspective - in a post on StackOverflow. In particular if you’ve an odd API_BASE_URL set this will cause issues.
Hope this helps get the workaround working for you, if not try posting details with example code and hopefully someone can help.