2

i need to send some parameters to the API which accept as it seems only GET method...if i join parameters Im unable to send it through GET method and with POST method I'm getting 404 - not found for the call...

already tried different methods of joining parameters to the call but no luck

// Documentation - https://coinmarketcap.com/api/documentation/v1/#section/Quick-Start-Guide
String apiKey = "707e6117-e462-4de3-9748-98ab6a467f0c"; // my temp key feel free to use it 
HttpURLConnection urlConnection = null;
URL url = new URL("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("X-CMC_PRO_API_KEY", apiKey);
Map<String, String> parameters = new HashMap<>();
parameters.put("start", "1");
parameters.put("limit", "5000");
parameters.put("convert", "USD");
urlConnection.setDoOutput(true);
DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
out.flush();
out.close();
urlConnection.connect();
int status = urlConnection.getResponseCode();
String message = urlConnection.getResponseMessage();

I would like to have results from API

2 Answers 2

1

The documentation only mention GET method. Add the parameters as standard HTTP GET parameters:

    String apiKey = "707e6117-e462-4de3-9748-98ab6a467f0c"; 
    final String request = "start=1&limit=500&convert=USD"; 
    HttpURLConnection urlConnection = null;
    URL url = new URL("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?" + request);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setReadTimeout(10000);
    urlConnection.setConnectTimeout(15000);
    urlConnection.setRequestProperty("Content-Type", "application/json");
    urlConnection.setRequestProperty("X-CMC_PRO_API_KEY", apiKey);

    try (BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())))
    {
        String line = br.readLine();

        while (line != null)
        {
            System.out.println(line);
            line = br.readLine();
        }

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you guys! bouth of you... it finally wors:)
1
String apiKey = "707e6117-e462-4de3-9748-98ab6a467f0c"; // my temp key feel free to use it
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";

        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        map.add("start", "1");
        map.add("limit", "5000");
        map.add("convert", "USD");
        map.add("ReadTimeout", "10000");
        map.add("ConnectTimeout", "15000");
        map.add("X-CMC_PRO_API_KEY", apiKey);
        System.out.println(map);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            System.out.println(response.getBody());
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.