Acess api using hava httpurlconnection

Topic summary

A developer is attempting to access the Shopify API using Java’s HttpURLConnection but encountering issues. They’re trying to retrieve orders via a GET request to the orders.json endpoint.

Technical approach:

  • Setting the X-Shopify-Access-Token header for authentication
  • Configuring the request with application/json accept header
  • Using GET method with output stream enabled

Current problem:

  • Instead of receiving order data, they’re getting XML responses
  • Also experiencing a 406 response code (Not Acceptable)

Code concerns:

  • The urlParameters variable incorrectly formats the header as a curl command string (“-H "X-Shopify-Access-Token: my_token"”) rather than using setRequestProperty
  • Writing urlParameters to the output stream on a GET request is unnecessary and potentially problematic

The discussion appears incomplete, with the code snippet cut off mid-execution. The issue likely stems from improper header configuration or request formatting for the Shopify API endpoint.

Summarized with AI on November 25. AI used: claude-sonnet-4-5-20250929.

Hello,

I’m trying to access my shopify app using java httpurlconnection:

I tried:

String urlParameters  = "-H \"X-Shopify-Access-Token: my_token\""; 
 URL url = new URL(SHOPIFY_API+"orders.json ");
        
 HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
 conn.setDoOutput(true);
 conn.setRequestMethod("GET");
 conn.setRequestProperty("accept", "application/json");
 conn.setRequestProperty("X-Shopify-Access-Token",sl.getTOKEN());

        OutputStream os = conn.getOutputStream();
        os.write(urlParameters.getBytes());
        os.flush();
 
        InputStream is = null;
        
        if(conn.getResponseCode()>=200 && conn.getResponseCode()<=299) {
            is = conn.getInputStream();  
        } else {
            is = conn.getErrorStream();
        }

        BufferedReader token_data = new BufferedReader(new InputStreamReader(is));

        token_data.lines().forEach((lin) -> {
            data = lin;
            System.out.println(data);
        });

I get a 406 response code.

I also tried :

String urlParameters  = "-H \"X-Shopify-Access-Token: my_token\""; 
 URL url = new URL(SHOPIFY_API+"orders.json " + urlParameters);
        
  HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
  conn.setDoOutput(true);
  conn.setRequestMethod("GET");
  conn.setRequestProperty("accept", "application/json");

        InputStream is = null;
        
        if(conn.getResponseCode()>=200 && conn.getResponseCode()<=299) {
            is = conn.getInputStream();  
        } else {
            is = conn.getErrorStream();
        }

        BufferedReader token_data = new BufferedReader(new InputStreamReader(is));

        token_data.lines().forEach((lin) -> {
            data = lin;
            System.out.println(data);
        });

But instead of getting my orders i get an xml:


Can anyone help me?