Jquery Ajax Get request

Continuously getting {“errors”:[{“error”:“invalid-authorization-header”,“type”:“ch:service”}]} when using Jqeury Ajax request.

My code here: I am sending the api key in a var called chApiKey

function lookUpCompanyHouseById(company_number){

			$.ajax({
					url: "https://api.companieshouse.gov.uk/company/company_number/",
					type: "GET",
					crossDomain: true,
					contentType: "application/json",
					data: company_number,
					dataType:'json',
					success: function(data){
						//Response text
						alert(data);					
					},
					beforeSend: function (xhr) {
						xhr.withCredentials = true;
						xhr.setRequestHeader ("Authorization", make_base_auth(encodeURIComponent(chApiKey)))
					},
					error:function(){
						//Gat failure
						alert("failure");
						
					}
			});
			
		}
		
		function make_base_auth(user) {
			var tok = user;
			var hash = btoa(tok);
			return "Basic " + hash;
		}

It looks like you’re missing a ‘:’ at the end of the API key before encoding it. With HTTP basic authentication the ‘:’ is necessary, even when the password is blank. So something like var tok = user + ":"; should work.

Thanks for your reply I have added the ‘’:" as mentioned and now I get 404 not found? This is the correct url right? https://api.companieshouse.gov.uk/company/company_number/?05961102 Failed to load resource: the server responded with a status of 404 (Not Found)

05961102 being the company I am searching as a test.

After further digging it seems its sending the auth details now and after further inspection I am getting this back {type: “ch:service”, error: “company-profile-not-found”}

However using the online api to test using the same number it finds it?

I think the URL should just be: https://api.companieshouse.gov.uk/company/05961102

Yes, the URL is https://api.companieshouse.gov.uk/company/05961102

I am having CORS issue while making above Jquery Ajax call

“Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.companieshouse.gov.uk/search/companies. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).”

How can I get the required access permissions?

In your API key registration, did you configured the domain (s) that you are making the Ajax request from? If you do not do this, our API will not negotiate permission with the CORS protection implemented by your client.

Generally, an API with CORS protection will compare the Origin: header sent by a JS client against an authorisation list. If a match is made, an Access-Control-Allow-Origin: matched-domain is sent back which authorises the client.

Log onto the developer hub and edit your API key to review and/or correct the details. If everything is as it should be, come back with more info and we’ll take a look.

I am getting this error No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://rcidirect.co.uk:8109’ is therefore not allowed access. The response had HTTP status code 401

But that domain is in my application specification !

What am I doing wrong ?

These are my request headers:
Request URL:https://api.companieshouse.gov.uk/company/05961102
Request Method:GET
Status Code:401 Unauthorized
GET /company/05961102 HTTP/1.1
Host: api.companieshouse.gov.uk
Connection: keep-alive
Accept: application/json, text/javascript, /; q=0.01
Origin: http://rcidirect.co.uk:8109
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36
Authorization: Basic <>:
Content-Type: application/json
Referer: http://rcidirect.co.uk:8109/RCIDirect/site/initFinancingVN.do?process=true
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

Authorization line should read:
Authorization Basic mykey:

just worked this out, works now, should be, pseudo code:
Authorization: Basic base64encode(myAPIkey+’:’)

Great news, thanks for letting us know

Hi,

I have been trying to get it working from last 2 days but it doesn’t work. I am using the same example and I am sending the request from the JavaScript Domain which I registered at Companies house.

It would be great if someone send a full complete example. It shouldn’t be that hard connecting to REST API in 2016. I have even used Fiddler to send a request but I keep getting 401 Unauthorized error.

Thanks

1 Like