SAS PROC HTTP API not working

Hi All

I am trying to use the following code in SAS to get company information:

filename resp temp;

%let fullurl= https://api.companieshouse.gov.uk/search/companies/ ;

proc http
url="&fullurl api_key=mykey"
method= "GET"
out=resp
ct="application/json"
;
run;

libname test JSON fileref=resp;

The correct API key has been replaced as “mykey” above and I have verified that my API key works through a macro enabled worksheet connecting to companies house.

I am however getting 401 unauthorized error in SAS with the above, can anybody help?

Thanks!

Are you specifying http Basic authorisation as per the Companies House documentation on authorisation?

I don’t use SAS but does the line:

url="&fullurl api_key=mykey"

… actually do this? Online documentation for the current version of SAS (9.4) for the PROC http statement doesn’t show any additional parameters on the url part but does suggest that you can specify http Basic authorisation using AUTH_BASIC - in which case your API key should go in WEBUSERNAME and WEBPASSWORD would be blank / ignored.

AUTH_BASIC is apparently available from SAS 9.4M3. However if you’re using a lower version of SAS you will need to make your own authentication header. The SAS docs say:

HEADERIN=fileref-to-request-header-file
specifies a fileref to a text file that contains one line per request header in the format key:value.
Note: Beginning with SAS 9.4M3, this option is supported only for compatibility with previous versions of SAS software. Use the HEADERS Statement instead of HEADERIN=

For how to construct the header see the CH docs and examples on the forum like the one following (see response from csmith):

Hi voracity

We have added AUTH_BASIC, WEBUSERNAME as the API key and WEBPASSWORD blank as you mentioned and it worked fine!!

Thanks a lot for your help.

Kind regards