I’ve been using a couple of endpoints successfully (/search, and /company) but I’m struggling with advanced-search. I’m getting a 403 which my browser is reporting as a CORS error. I can get the exact same URL to work in the Insomnia REST client, and I’ve checked my authorization header is being sent from my application. Any suggstions?
Thanks for replying. The weird thing is, it works from Insomnia (similar to Postman). So the API is working … but there must be some quirk that doesn’t happen in Insomnia, but does happen in my JS code. FWIW, I just literally flipped the code to a different API call (/search?q=…) and it worked fine. So I think my headers are OK
I have concluded that the API for advanced search is not returning the correct response headers.
REST clients like Insomnia and Postman are immune to this as they don’t enforce CORS. The API is returning the right payload in the response, hence it all looks OK in REST clients. But browsers enforce CORS and so they refuse to accept the response.
I believe at least one header is missing - Access-Control-Allow-Origin. There could be others.
Note - this is just my best guess; I could be wrong
Interesting. All the URIs do work when called from non-javascript environments (which is how we use the system). I’ve just seen various posts on the forum specifically related to the advanced search endpoint and CORS issues. That endpoint was added much later than the others and has some differences so it’s entirely possible it has its own “features”.
If it’s exactly the same URL you’re calling in Insomnia and your API I wonder if the environment is different in those two cases e.g. is the effective domain / IP different (Companies House check this I think) and / do - or how do - the http headers differ?
I note that access-control-allow-origin is missing, but then again, it’s also missing in Insomnia for other queries that work in a browser - so I don’t think we can read too much into that
But for those (working) queries, I can see access-control-allow-origin in the browser response headers, and it is set to my origin domain (because I have enabled that origin in my API definition registered against my application).
Anyway … I have to knock it on the head for today; hopefully someone at the CH team will see this and respond soon
Final note from me (for now!) on this, for the benefit of anyone else trying to use Advanced Search:
If you are calling it directly from JS in a browser container I think you’ll need to work around this problem by adopting a better architecture and delegating the call to a back-end service (or some other kind of proxy). My approach was always just a prototype / experiment, but it’s not a good template for production because the browser/JS exposes code and run-time data (just go into developer tools in the browser and bingo!). In this way a bad actor could easily see the API Key in use and effectively steal the credentials. Better to protect this with a server side wrapper around the calls to companies house, and this will (I expect) avoid the CORS issue. I’ll post again when I can confirm this.