1

I want to program a CURL Webservice Client with Java but it doesn't work.

I want make a client of this

curl "https://api.esios.ree.es/archives" -X GET \
-H "Accept: application/json; application/vnd.esios-api-v1+json" \
-H "Content-Type: application/json" \
-H "Host: api.esios.ree.es" \
-H "Authorization: Token token=\"96c56fcd69dd5c29f569ab3ea9298b37151a1ee488a1830d353babad3ec90fd7\"" \
-H "Cookie: "

I make a little program using com.sun.jersey.api.client.Client and ClientResponse but fail. The example is that:

Client client = Client.create();
WebResource webResource =client.resource("https://api.esios.ree.es/archive");
MultivaluedMap queryParams = new MultivaluedMapImpl();
queryParams.add("Accept", "application/json; application/vnd.esios-api-v1+json");
queryParams.add("Content-Type", "application/json");
queryParams.add("Host", "api.esios.ree.es");
queryParams.add("Authorization", "Token token=\"96c56fcd69dd5c29f569ab3ea9298b37151a1ee488a1830d353babad3ec90fd7\"");
queryParams.add("Cookie", " ");
ClientResponse response = webResource.queryParams(queryParams).accept("application/json").get(ClientResponse.class);
if (response.getStatus() != 200) {
   throw new RuntimeException("Failed : HTTP error code : "
    + response.getStatus());
}

The code response Failed : HTTP error code : 401. Thanks a lot for try to help me.

3
  • 1
    looks like you are sending those as queryParams, not headers. see webResource.header(name,value) Commented Jul 5, 2016 at 7:14
  • 1
    Also I spotted that you use https://api.esios.ree.es/archives in first code sample and https://api.esios.ree.es/archive in the second Commented Jul 5, 2016 at 7:22
  • if that token is sensitive, you might not want to have posted it in the code Commented Jul 5, 2016 at 7:28

2 Answers 2

2

You need to send those values as headers, not query parameters.

ClientResponse response = webResource.header("Accept", "application/json; application/vnd.esios-api-v1+json")
                .header("Content-Type", "application/json")
                .header("Host", "api.esios.ree.es")
                .header("Authorization", "Token token=\"TOKEN\"")
                .header("Cookie", " ")
                .get(ClientResponse.class);

I'v removed the accept() method as we are setting this with the header() method.

And as pointed out by Ruslan, check you are calling the correct endpoint.

Sign up to request clarification or add additional context in comments.

Comments

-1

Attention! The client.resource route ("https://api.esios.ree.es/archive"); is incorrect has to be client.resource ("https://api.esios.ree.es/archives"); if not return 404 error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.