Search code examples
javarestgatewayget-requesthttp-status-code-504

Why does GET Request always give 200 on PostMan but gives 504 in Java App?


I'm currently working on integrating with a company's API, and I am able to hit their Order Retrieval endpoint in PostMan, getting a 200OK status, but in my java app, I've tried hitting the endpoint with OkHttp3, Unirest, and even my own HttpReq library, but no matter which library I use, I end up getting

<head><title>504 Gateway Time-out</title></head>
<body bgcolor="white">
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>nginx/1.4.6 (Ubuntu)</center>
</body>
</html>

The headers, params, url for my java HttpRequest and PostMan are identical, as I copied the URL, params, headers from the result of printing out all the variables in Java, into PostMan. I've even tried using the Java snippets PostMan provides (HttpOk3/UniRest), but am still getting a 504. I've also tried setting timeouts to infinity, etc. What could I be doing wrong?

Here's the java HttpRequest I made with Unirest:

         try {
            Unirest.setTimeouts(30000, 100000);
            HttpResponse<String> response = Unirest.get(this.getOrderUrl)
                    .queryString("access_token", config.getAccessToken())
                    .queryString("id", circleGraphicsOrderId)
                    .header("content-type", "application/json")
                    .header("cache-control", "no-cache")
                    .header("authorization", "Basic Og==")
                    .asString();
        } catch (UnirestException e) {
            e.printStackTrace();
        }

Here's the API credentials if any of you would like to test with me:

(Params built into the url) URL = https://rest.cgorders.com/api/orders/order?access_token=97484431a4525ed2b294b699a3d2f202&id=15755354

The Params were: {access_token:97484431a4525ed2b294b699a3d2f202, id:15755354}

EDIT: Resolved! I was using wacky HttpRequest Libraries because our company code had a bunch of use cases with such libraries. Using the java provided one as Markspace wrote below resolved the issue.


Solution

  • The following works for me. What do you need to change to get it working how you want?

    public class GetRequest {
    
       public static void main( String[] args ) {
          System.out.println( new GetRequest().get( "" ) );
       }
    
       public String get( String search ) {
    
          try {
             URL url = new URL( "https://rest.cgorders.com/api/orders/order?access_token=97484431a4525ed2b294b699a3d2f202&id=15755354" );
             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
             conn.setRequestMethod( "GET" );
             conn.setRequestProperty( "Accept", "application/json" );
    
             conn.connect();
    
             BufferedReader br = new BufferedReader( new InputStreamReader(
                     conn.getInputStream(), "UTF-8" ) );
    
             StringBuilder sb = new StringBuilder( 2048 );
             for( String line; (line = br.readLine()) != null; ) {
                sb.append( line );
             }
             conn.disconnect();
    
             return sb.toString();
    
          } catch( IOException ex ) {
             Logger.getLogger( Pixabay.class.getName() ).log( Level.SEVERE, null, ex );
             return ex.toString();
          }
    
       }
    }