user3547469 user3547469 - 3 months ago 16
Java Question

CURL and JAVA error

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

I want make a client of this

curl "" -X GET \
-H "Accept: application/json; application/vnd.esios-api-v1+json" \
-H "Content-Type: application/json" \
-H "Host:" \
-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("");
MultivaluedMap queryParams = new MultivaluedMapImpl();
queryParams.add("Accept", "application/json; application/vnd.esios-api-v1+json");
queryParams.add("Content-Type", "application/json");
queryParams.add("Host", "");
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.


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", "")
                .header("Authorization", "Token token=\"TOKEN\"")
                .header("Cookie", " ")

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.