Search code examples
javaazureodataolingo

Trying to connect to datamarket returns exception


I'm trying to connect to Azure data market, it's an odata repository. I'm using the latest Olingo library, r4.2.0. The following code :

String serviceUrl = "https://api.datamarket.azure.com/DataGovUK/MetOfficeWeatherOpenData/v1/";

ODataClient client = ODataClientFactory.getClient();
ODataServiceDocumentRequest req = client.getRetrieveRequestFactory().getServiceDocumentRequest(serviceUrl);
req.setAccept("application/json;application/xml;odata.metadata=full");
req.setContentType("application/json;application/xml;odata.metadata=full");
ODataRetrieveResponse res = req.execute();

returns an exception

 org.apache.olingo.client.api.communication.ODataClientErrorException: null [HTTP/1.1 415 Unsupported Media Type]

The server returns :

<?xml version="1.0" encoding="utf-8"?>
<m:error mlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
   <m:code />
   <m:message xml:lang="en-US">
       Unsupported media type requested.
   </m:message>
</m:error>

Somebody tried to connect to Azure data market with this library or another with Java?


Solution

  • Try to set the request headers Accept & Content-Type with only JSON or XML, not both. Please see below.

    req.setAccept("application/json");
    req.setContentType("application/json;odata.metadata=full");
    

    Or

    req.setAccept("application/atom+xml,application/xml");
    req.setContentType("application/atom+xml,application/xml;odata.metadata=full");
    

    and for the authorization

    req.addCustomHeader("Authorization", "Basic " + getAccountKey());
    

    where account key as described in this other stackoverflow post is:

    public String getAccountKey()
    {
        String accountKey = "My Microsoft Azure Account Key";
        byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
        String accountKeyEnc = new String(accountKeyBytes);
        return accountKeyEnc;
    }