# HTML Response to a Beta Request

**URL:** <http://forum.companieshouse.gov.uk/t/html-response-to-a-beta-request/331>\
**Category:** API Issues\
**Created:** [November 2, 2015, 5:20pm UTC](http://forum.companieshouse.gov.uk/t/html-response-to-a-beta-request/331 "2015-11-02T17:20:46Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![csmith](http://forum.companieshouse.gov.uk/user_avatar/forum.companieshouse.gov.uk/csmith/32/8_2.png) [@csmith](http://forum.companieshouse.gov.uk/u/csmith)\
**Post date:** [November 17, 2015, 3:12pm UTC](http://forum.companieshouse.gov.uk/t/html-response-to-a-beta-request/331/8 "2015-11-17T15:12:14Z")

</div>

@awais_techloyce123

Ah. Okay, there are several things wrong here:

1. You are sending your API requests to the WEBSITE.
2. Your authorisation header is wrong.

First, do not be confused that the URLs look **similar** for the API and the website. The API will return **data** encoded as JSON. The Website will of course return HTML, plus, if you scrape the website, you’ll no doubt have your IP blocked by our access control infrastructure…

Your problem stems from you not authenticating properly, hence the HTTP status code of “401 Unauthorised” you are getting.

If you search the forum (and I really do **urge** people to do this, as this has been answered many times before), there are several topics that show you how to correct your error. Most come down to users of the API not providing `HTTP Basic authentication`:

> **[Basic access authentication](https://en.wikipedia.org/wiki/Basic_access_authentication)**
>
> In the context of an HTTP transaction, basic access authentication is a method for an HTTP user agent (e.g. a web browser) to provide a user name and password when making a request. In basic HTTP authentication, a request contains a header field in the form of Authorization: Basic \<credentials\>, where \<credentials\> is the Base64 encoding of ID and password joined by a single colon :.
> It was originally implemented by Ari Luotonen at CERN in 1993 and defined in the HTTP 1.0 specification in 1996.
> ...

Quite a few HTTP clients natively handle Basic authentication, so for them it is a simple matter of setting the username and the password (which in our case is blank) on the client instance before making the call. For other clients, you have to set the Basic authentication header yourself.

We have posted a simple Perl example to demonstrate the Basic authentication mechanism if you have to set the header yourself. You should be able to translate the two important lines of Perl into two lines of Java to do the same. This example also shows how a `curl` request works, as it natively handles Basic authentication, demonstrating what headers are being sent, and what they contain:

> [@Getting invalid authorization](http://forum.aws.chdev.org/t/getting-invalid-authorization/41/7):
>
> The existing XML Gateway API employs a custom authentication mechanism unique to Companies House, and involves the complex MD5 hashing of passwords and transaction ID’s. The new Companies House REST API uses standard [Basic HTTP Authorisation][1]. This makes API key authentication as simple as the following curl command: curl -v -uYOUR\_APIKEY\_FOLLOWED\_BY\_A\_COLON: https://api.companiseshouse.gov.uk/company/00002065 You may have to do a little more work in code, depending on whether your client…

Finally, a quick web search for `URLConnection BASIC authentication` returns the following first hit, which actually provides the two lines of Java I mentioned above:

> <https://stackoverflow.com/questions/7019997/preemptive-basic-auth-with-httpurlconnection>

That and the Perl example should get you up and running. Perhaps you could come back and post your corrected code snippet here, when you get it working, to close this topic off?

Hope that helps. Good luck!  
Regards

---

_[View the full topic](http://forum.companieshouse.gov.uk/t/html-response-to-a-beta-request/331)._
