-1

I'm new to REST API and trying to work with the Get Quotes method in the Tradier API to retrieve market data about a specific company. The request is giving me back response 400 with my code below. Here is a link to the API documentation: https://documentation.tradier.com/brokerage-api/markets/get-quotes

try {
        // MAKE REQUEST TO API
        URL url = new URL("https://sandbox.tradier.com/v1/markets/quotes");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Authorization", "Bearer <token>");
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/json");
        connection.addRequestProperty("symbols", "AAPL");
        connection.connect();

        // GET INPUT STREAM RESPONSE FROM REQUEST
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

        // TAKING BITS AND BYTES FROM BUFFERED READER TO BUILD IT INTO A STRING OBJECT
        StringBuilder json = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            json.append(line);
        }

        JSONObject obj = new JSONObject(json.toString());
        System.out.println(obj);

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
Paul
  • 19,704
  • 14
  • 78
  • 96
TRG311
  • 1
  • 2
  • Those docs show you a Java example, why not copy it? – OneCricketeer Nov 21 '20 at 18:24
  • I will try to do it that way if I can't figure it out using HttpURLConnection. I was trying to do it this way because this is the way my professor demonstrated it with the API she was using in class – TRG311 Nov 21 '20 at 18:27
  • 2
    What is the message returned with the 400 error? – Paul Nov 21 '20 at 18:27
  • Note: you might want to remove your api token from the question – OneCricketeer Nov 21 '20 at 18:28
  • The message says: Server returned HTTP response code: 400 for URL: https://sandbox.tradier.com/v1/market/quotes – TRG311 Nov 21 '20 at 18:28
  • I'm doing this in a Spring Boot project btw – TRG311 Nov 21 '20 at 18:29
  • What happens if you just add `?symbols=AAPL`, to the end of the url – OneCricketeer Nov 21 '20 at 18:33
  • You're setting the required parameter "symbols" as a request property. See the example at the link you provided which has the URL as "v1/markets/quotes?symbols=AAPL" – Paul Nov 21 '20 at 18:34
  • Thanks you guys it worked when I just put the ?symbols=AAPL into the url. I didn't think to do that because in the Java example they demonstrate in the docs they add it as a parameter. – TRG311 Nov 21 '20 at 18:41
  • 2
    Does this answer your question? [How to add parameters to HttpURLConnection using POST using NameValuePair](https://stackoverflow.com/questions/9767952/how-to-add-parameters-to-httpurlconnection-using-post-using-namevaluepair) – priyranjan Nov 22 '20 at 12:51

1 Answers1

0

(1st Method)

change your url to :--

"https://api.tradier.com/v1/markets/quotes?symbols=AAPL,VXX190517P00016000&greeks=false" ( I mean manually add it)

(2nd Method)

OR,

add RequestParam like below code:----

try{

 URL url = new URL("https://sandbox.tradier.com/v1/markets/quotes");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Authorization", "Bearer <token>");
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/json");
    Uri.Builder builder = new Uri.Builder()
            .appendQueryParameter("symbols", "AAPL,VXX190517P00016000")
            .appendQueryParameter("greeks", "false");
    String query = builder.build().getEncodedQuery();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();

connection.connect();
 // GET INPUT STREAM RESPONSE FROM REQUEST
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

        // TAKING BITS AND BYTES FROM BUFFERED READER TO BUILD IT INTO A STRING OBJECT
        StringBuilder json = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            json.append(line);
        }

        JSONObject obj = new JSONObject(json.toString());
        System.out.println(obj);

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

3rd Method

try{
 final HttpUriRequest request = RequestBuilder
        .get("https://api.tradier.com/v1/markets/quotes")
        .addHeader("Authorization", "Bearer <TOKEN>")
        .addHeader("Accept", "application/json")
        .addParameter("symbols", "AAPL,VXX190517P00016000")
        .addParameter("greeks", "false")
        .build();

    final HttpResponse response = HttpClientBuilder.create().build().execute(request);
    final String jsonString = EntityUtils.toString(response.getEntity());
    final JsonNode json = new ObjectMapper().readTree(jsonString);
    
    System.out.println(response.getStatusLine().getStatusCode());
    System.out.println(json);}
catch(Exception ex){
ex.printStack();
}
priyranjan
  • 674
  • 6
  • 15