Error 400 using Flutter/Dart

Hey guys, I’m trying to fetch the API using flutter/dart for a web App, but I got 400 error.

Future fetchData() async {

final response = await http.get(

Uri.parse("https://api.company-information.service.gov.uk/company/06500244"),

headers: {

  HttpHeaders.authorizationHeader: "Basic ${apiKey}",

},

);

final jsonResponse = convert.json.decode(response.body);

if (response.statusCode == 200) {

print(jsonResponse.toString());

} else {

throw Exception(response.statusCode);

}
}

Any other information I need to pass other than apikey?

Thanks.

please help I have the same issue, :frowning:

Welcome.

A “400 error” - does this mean literally 400 or e.g. one of the http error codes e.g. 401 (what you see if you’ve got everything else right but you’re not authorised?)

Read the following then - if still stuck - try posting what you’re actually sending and receiving. (Using something like curl is great for this - simple and you can see the whole of the traffic). Don’t post your ACTUAL api key here though - censor it or use a dummy one!

Companies House use http Basic authentication. Instead of a username and password however they provide a “username” (API key) which is the password, and where the password would go is blank.

If you’re writing the http header yourself as it says in the documentation you will need to:

  1. concatenate your http basic username (API key), a : character and the password (nothing - so your string should end in “:”)
  2. Base-64 encode that whole string.
  3. The header string is now the string "Basic " (note space) plus the string from (2)

See the Companies House docs here. Or - more versions of the same - the 3rd party “CH Guide” on authentication.

Other issues you could have are if you’ve not registered for a live API key for streaming / data API or not registered your IP address / your javascript host with Companies House.

1 Like