Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET Request with Content-Type and Accept header with JAX-RS Jersey 2.2

Tags:

I try to access an open data web service which gives me traffic infos. Documentation says that requests have to be GET and need to contain Accept: application/json and Content-Type: application/json. I don't understand why they need the Content-Type but ok:

I tried to retrieve data with just the Accept: Header but I'm always getting a 415 Unsupported Media Type. Now I am currently trying it this way (but I'm not sure if I am really setting both headers correctly):

String entity = ClientBuilder.newClient().target(liveDataURI)
    .path(liveDataPath)
    .request(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON)
    .get(String.class);

As you see I am using Jersey 2.2 and I'm still getting a 415 Unsupported Media Type.

EDIT

So I got it to work but I don't understand why. Isn't accept(MediaType.APPLICATION_JSON) and header("Content-type","application/json") the same?

String responseEntity = ClientBuilder.newClient()
    .target(liveDataURI)
    .path(liveDataPath)
    .request(MediaType.APPLICATION_JSON)
    .header("Content-type", "application/json")
    .get(String.class);