JollyRoger
JollyRoger

Reputation: 847

How to enable cookies while sending a request with Java Jersey client?

I need to send a get request to an API and get the results. I am using Java, Jersey library in my project and I decided to use Jersey client in order to get the data. However, the API returns an error message which indicates that I should enable cookies to reach out this API. I can get correct response when try with applications like postman, or just with using a normal browser like chrome. But I could not find out how can I enable cookies in Java Jersey client object.

I searched to learn how to enable cookies in a Java Jersey client but I could not find any resource about it. So I could not try any solution.

My code is very simple:

    Client client = Client.create(); // Create jerseu client
    WebResource webResource = client.resource(BASEURI +  EXCHANGEINFO); // create web resource with a specific URI

    System.out.println(webResource 
            .accept("application/json")
            .get(ClientResponse.class)
            .getEntity(String.class)); // Write results to console

At the result of this request, I got the error that I mentioned above. How can I enable cookies while sending a request with Java Jersey client?

Upvotes: 1

Views: 1162

Answers (1)

Sambit
Sambit

Reputation: 8011

As per the discussion, I have gone through the api you have provided. Actually, the api provides a misleading message while making rest call. If you look into the details of the error message which is received from the api call, it says.

The owner of this website (api.pro.coinbase.com) has banned your access based on your browser's signature (4e0a3c06895d89af-ua21).

So what is the answer ? The api actually wants that the call should be made from browser and each browser sends a header called "User-Agent". See what is user agent. However, I have solved your problem, you can check the complete code below.

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class TestGetCallByJersey {
  public static void main(String[] args) {
    String resourceUri = "https://api.pro.coinbase.com/products";
    try {
      Client client = Client.create();
      WebResource webResource = client.resource(resourceUri);
      ClientResponse response =
          webResource
              .accept("application/json")
              .header("User-Agent", "Mozilla/5.0")
              .get(ClientResponse.class);

      System.out.println("response status = " + response.getStatus());
      String result = response.getEntity(String.class);
      System.out.println("Output from api call .... \n" + result);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Currently, I have tested in Java 8 and I used the following jar file.

jersey-client version 1.8 If you are using, Maven, you can include the following dependency in pom.xml.

<dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.8</version>
        </dependency>

Upvotes: 1

Related Questions