skip to Main Content

I am attempting to write a private app for the Shopify API in Java. I’m using the org.apache.http library to write my requests. Below is my current request:

    CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build()).build();
    HttpPut putRequest = new HttpPut(request);
    StringEntity params;

    try {
        params = new StringEntity(json);
        putRequest.addHeader("X-Shopify-Access-Token", Constants.getShopifySharedSecret());
        putRequest.setEntity(params);

        CloseableHttpResponse response = httpClient.execute(putRequest);
        HttpEntity entity = response.getEntity();

        System.out.println("Title Change Request Output: ");
        System.out.println(EntityUtils.toString(entity));

        response.close();
        httpClient.close();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return;

This is returning an HTML page asking for login. After searching the Shopify forums, it appears that when doing POST or PUT requests to Shopify, you’re not allowed to send any cookies otherwise the login page is returned. So my question is, is there any way to ensure that CloseableHttpClient will NOT send any cookies?

2

Answers


  1. You can bind a custom Cookie store to a local HTTP Context and send that in the request

     CloseableHttpClient httpclient = HttpClients.createDefault();
    
            // Create a local instance of cookie store
            CookieStore cookieStore = new BasicCookieStore();
    
            // Create local HTTP context
            HttpClientContext localContext = HttpClientContext.create();
            // Bind custom cookie store to the local context
            localContext.setCookieStore(cookieStore);
    
            //Your PUT request goes here
            CloseableHttpResponse response = httpClient.execute(putRequest,localContext);
    
    Login or Signup to reply.
  2. One way of stopping HttpClient from sending cookies to the target host is, well, by disabling cooking management

    CloseableHttpClient client = HttpClientBuilder.create()
            .disableCookieManagement()
            .build();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search