I am accessing Company House Api for searching Company Details. For accesiing API I have created a webservice in C#
public string GetCompanyDetailsByNumber(string Email, string password, string strCompanyNumber)
{
string theURL = "https://api.companieshouse.gov.uk";
var url = theURL + "/search/companies?q= '" + strCompanyNumber.Trim() + "' ";
HttpWebRequest client = (HttpWebRequest)WebRequest.Create(new Uri(url));
string results = string.Empty;
client.Method = "GET";
client.ContentType = "application/json";
client.Host = "api.companieshouse.gov.uk";
client.Headers.Add("Authorization", "Basic MY apI kEY");
client.Accept = "text/plain,*/*; q=0.01";
HttpWebResponse response;
using (response = client.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
results = reader.ReadToEnd();
// DataTable tester = (DataTable)JsonConvert.DeserializeObject( results, (typeof(DataTable)));
}
return results;
}
Above returns data when its search by Name but do not reutrn data when search by Company Number
Kindly guide
First off, if you know the company number (the full 8 characters) you do not need to search, you can get the company data directly
BUT I just followed the documentation on https://developer.companieshouse.gov.uk/api/docs/search/companies/companysearch.html
and called :-
GET /search/companies?q=00006400
and got back :-
{
“start_index”: 0,
“items_per_page”: 20,
“items”: [
{
“company_type”: “private-limited-shares-section-30-exemption”,
“links”: {
“self”: “/company/00006400”
},
“address”: {
“address_line_1”: “Bressenden Place”,
“country”: “England”,
“postal_code”: “SW1E 5DH”,
“premises”: "10 ",
“locality”: “London”
},
“description”: “00006400 - Incorporated on 26 June 1872”,
“company_status”: “active”,
“date_of_creation”: “1872-06-26”,
“title”: “THE GIRLS’ DAY SCHOOL TRUST”,
“kind”: “searchresults#company”,
“company_number”: “00006400”,
“description_identifier”: [
“incorporated-on”
],
“address_snippet”: “10 Bressenden Place, London, England, SW1E 5DH”
}
],
“page_number”: 1,
“kind”: “search#companies”,
“total_results”: 1
}
I am creating URl https://api.companieshouse.gov.uk/company/‘CompanyNumber’ but its not working throwing exception
{“error”:“Empty Authorization header”,“type”:“ch:service”}
Kindly Guide
The code you posted above contains:
client.Headers.Add(“Authorization”, “Basic MY apI kEY”);
You need something like (spelling it out):
String username = "MY apI kEY";
String password = "";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
client.Headers.Add("Authorization", "Basic " + encoded);
Since the error is “Empty Authorization header” then that suggests the source of the problem e.g. there’s a problem with the value you’re sending. You need to have one space after “Basic” - at least that works for me - and then base64 encode your api key. This includes the ‘:’ between the username [API key] and the [blank] password.
Not being a C# coder I used Google to look up client.Headers.Add() and authentication and this found the following on StackOverflow:
…which references also:
…which is where I stole the code above.