1
$\begingroup$

I am a newbie with RESTful APIs and have never tried accessing one from inside Mathematica. I am trying to access the Companies House API, in the UK. The following command works fine from the Terminal command line (company number 00000006 is just a number a picked at random):

curl -X GET -u My_API_Key: https://api.companieshouse.gov.uk/company/{00000006}

I entered the following code in a Mathematica notebook, following the help example:

urlRoot = "https://api.companieshouse.gov.uk/company/";
company = "{00000006}";
myKey = "My_API_Key";
requestURI = URLBuild[{urlRoot, myKey, company}]

with my actual key in place of My_API_Key. The URI created is this:

https://api.companieshouse.gov.uk/company/My_API_Key/%7B00000006%7D

When I execute this command:

response = URLExecute[requestURI, "RawJSON"]

I get the dialog into which I enter My_API_Key and some blank spaces for the password. That's because the API documentation says to do this, or at least this is what I have understood. The result is this:

<|"error" -> "Empty Authorization header", "type" -> "ch:service"|>

What am I doing wrong? Very sorry if this is a really simple/dumb question, I have never tried doing this before... Thanks

$\endgroup$
7
  • $\begingroup$ Can you try this? URLExecute@ HTTPRequest[ "https://api.companieshouse.gov.uk/company/{00000006}", <| "Headers" -> {"Authorization" -> ExportString["my_api_key:", "Base64"]}|>] - make sure you leave the : in place after you put in your API key. $\endgroup$
    – Carl Lange
    Feb 8 '20 at 20:58
  • $\begingroup$ Well, I got a different error message. The dialog came up anyway, and after entering the API key again and spaces for the password the response was {"error" -> "Invalid Authorization", "type" -> "ch:service"} $\endgroup$
    – pdini
    Feb 8 '20 at 22:52
  • $\begingroup$ I'm curious - what happens if you don't put in anything in that box? $\endgroup$
    – Carl Lange
    Feb 8 '20 at 23:17
  • $\begingroup$ @CarlLange, if I press Submit without entering username and/or password the message This field is required appears below each/either. There are no other buttons to press. $\endgroup$
    – pdini
    Feb 9 '20 at 9:19
  • $\begingroup$ Other possibly relevant info: the text in the dialog is You are attempting to read from the domain live-chs-apichgovuk-248537452.eu-west-2.elb.amazonaws.com The server is requesting authentication. If I click on that link another dialog opens up with the further msg: The file "/Applications/Mathematica12.app/Contents/live-chs-apichgovuk-248537452.eu-west-2.elb.amazonaws.com" could not be found. So it looks like I am missing some kind of local authentication file. $\endgroup$
    – pdini
    Feb 9 '20 at 9:29
3
$\begingroup$

We will generate our own Authorization header, because this API uses a slightly obtuse method. The API uses what's known as "Basic Authentication", which is essentially "Basic " + Base64["username:password"]. Since the password doesn't appear to matter for this API, we can generate our header like so.

auth = StringJoin["Basic ", 
  ExportString["MYKEY:", "Base64"]]

It's important to leave the : in place when you paste in your key.

And now we can read the results from the API:

URLExecute[
 HTTPRequest[
  "https://api.companieshouse.gov.uk/company/00000006", <|
   "Headers" -> {"Authorization" -> auth}|>]]

There are some weirdnesses here that caused your issue. First, the API uses basic authentication strangely. Normally for an API, you would pass an Authorization header with just your API key, rather than needing to encode it like a basic authentication header. As well, it seems like WL has some vaguely-defined behaviour with basic authentication. The Authorization symbol exists (as does that dialog that you came up against), but I actually can't make it work correctly. Seems like there's a bug in the system here - and if nothing else, it's poorly documented.

$\endgroup$
1
  • $\begingroup$ Thank you so much @CarlLange, it works! :) $\endgroup$
    – pdini
    Feb 9 '20 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.