Working cURL example?

Hi there,

just testing how to use the cURL request with the API but it doesn’t seem to working (I may be doing it wrong). I’m using Linux Mint Terminal and running this command:

curl -uMY_API_KEY -H"Content-Type: application/json" -X POST -d'{"q":"tre"}' https://api.companieshouse.gov.uk/search/companies

(MY_API_KEY is my actual API key).

I get this response: Enter host password for user 'MY_API_KEY'
There doesn’t seem to be anywhere (obvious) that I could find a host password, I tried both entering without a password and entering my login password used for my account.

After this (both give same response) I get this:

 Trying 194.75.26.22
 Connected to api.companieshouse.gov.uk (194.75.36.22) port 443 (#0)
 found 173 certificates in /etc/ssl/certs/ca-certificates.crt
 found 704 certificates in /etc/ssl/certs
 ALPN, offering http/1.1
 SSL connection using TLS1.2 / RSA_3DES_EDE_CBC_SHA1
 server certificate verification OK
server certificate status verification SKIPPED
common name: *.companieshouse.gov.uk (matched)
server certificate expiration date OK
server certificate activation date OK
certificate public key: RSA
certificate version: #3
subject: C=GB, ST=SOUTH GLAMORGAN,L=CARDIFF,O=Companies House,OU=IT Infrastructure,CN=*.companieshouse.gov.uk
start date: Thu, 09 Jun 2016 00:00:00 GMT
expire date: Sat, 09 Jun 2018 23:59:59 GMT
issuer: C=US,O=GeoTrust Inc.,CN=GeoTrust SSL CA - G3
compression: NULL
ALPN, server dod not agree to a protocol 
Server auth using Basic with user 'MY_API_KEY'
POST /search/companies HTTP/1.1
Host: api.companieshouse.gov.uk
Auhorization: Basic 'SOME_ENCRYPTED_KEY'
User-Agent: curl/7.47.0
Accept: */*
Content-Type: application/json
Content-Length: 11

upload completely sent off: 11 out of 11 bytes
HTTP/1.1 404 Not Found
Date: Tue, 11 Apr 2017 10:08:42 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-Length: 2
Pragma: no-cache
Content-Type: application/json
Access-Control-Expose-Headers: Location,www-authenticate,cache-control,pragma,content-type,expires,last-modified

Connection #0 to host api.companieshouse.gov.uk left intact

The two lines that stand out most to me are:

HTTP/1.1 404 Not Found
ALPN, server dod not agree to a protocol 

both these seem like a potential cause to the cURL failure - anything I’m doing or your end?

You may have spotted this already but you’re missing a “:” after the API key. API uses HTTP Basic Authentication and the API key is actually the username part - there isn’t a password. So the form in curl is:

curl -uMY_API_KEY: -H"Content-Type: application/json" -X POST -d'{"q":"tre"}' https://api.companieshouse.gov.uk/search/companies

As mentioned - but not especially obviously - in https://developer.companieshouse.gov.uk/api/docs/index/gettingStarted/apikey_authorisation.html.

Hope this helps!
BTW for me at least I don’t normally need to give the content type as JSON is returned by default. So:

 curl -uMY_API_KEY: -I "https://api.companieshouse.gov.uk/search/companies?q=tre"

…would work (quotes as for Windows). If you’re requesting a document from the documents API or want to use the versioning system (which I’m not 100% sure is operational?) then you’d want to supply a type.

1 Like

Hi,

Ah - I hadn’t spotted that at all! haha thanks - with the json curl request it didn’t work but using ‘i’ with query params seemed to work - doesn’t return company data though, it returns:

*   Trying 62.254.241.85...
* Connected to api.companieshouse.gov.uk (62.254.241.85) port 443 (#0)
* found 173 certificates in /etc/ssl/certs/ca-certificates.crt
* found 704 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* SSL connection using TLS1.2 / RSA_3DES_EDE_CBC_SHA1
* 	 server certificate verification OK
* 	 server certificate status verification SKIPPED
* 	 common name: *.companieshouse.gov.uk (matched)
* 	 server certificate expiration date OK
* 	 server certificate activation date OK
* 	 certificate public key: RSA
* 	 certificate version: #3
* 	 subject: C=GB,ST=SOUTH GLAMORGAN,L=Cardiff,O=Companies House,OU=IT Infrastructure,CN=*.companieshouse.gov.uk
* 	 start date: Thu, 09 Jun 2016 00:00:00 GMT
* 	 expire date: Sat, 09 Jun 2018 23:59:59 GMT
* 	 issuer: C=US,O=GeoTrust Inc.,CN=GeoTrust SSL CA - G3
* 	 compression: NULL
* ALPN, server did not agree to a protocol
* Server auth using Basic with user 'MY_API_KEY'
> HEAD /search/companies?q=tre HTTP/1.1
> Host: api.companieshouse.gov.uk
> Authorization: Basic 'SOME ENCRYPTED KEY'
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Tue, 11 Apr 2017 11:04:52 GMT
Date: Tue, 11 Apr 2017 11:04:52 GMT
< X-Ratelimit-Remain: 599
X-Ratelimit-Remain: 599
< X-Ratelimit-Reset: 1491908992
X-Ratelimit-Reset: 1491908992
< X-Ratelimit-Limit: 600
X-Ratelimit-Limit: 600
< Content-Length: 12231
Content-Length: 12231
< Content-Type: application/json
Content-Type: application/json
< Access-Control-Expose-Headers: Location,www-authenticate,cache-control,pragma,content-type,expires,last-modified
Access-Control-Expose-Headers: Location,www-authenticate,cache-control,pragma,content-type,expires,last-modified
< Pragma: no-cache
Pragma: no-cache
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0

< 
* Connection #0 to host api.companieshouse.gov.uk left intact

not sure if it’s what’s meant to be shown - but definitely beats the 404 thanks :slight_smile:

Oh, apologies, pasted wrong line - the -I argument is just to show header (which is informative, of course…) what I meant was:

curl -uMY_API_KEY: "https://api.companieshouse.gov.uk/search/companies?q=glaxo"

…(“glaxo” just becuase you’ll definitely get back search results).

Then you can go on e.g.

curl -uMY_API_KEY: "https://api.companieshouse.gov.uk/company/01047315"

…to get the company overview resource etc.

P.S. using http GET here as per info on the RESTful interface at https://developer.companieshouse.gov.uk/api/docs/index/gettingStarted/introductionToAPI.html
I don’t know if POST is acknowledged but it wouldn’t be necessary except for sending a message body with data e.g. if filing for a company.

1 Like

Hi,

ahaha no worries - I removed and it worked - thanks for your help :slight_smile: