0

I am new to API design, I am working on one project where I need to call currency exchange API from National Bank of Poland http://api.nbp.pl but I do not see any indication where I can find API ID. This development is on Spring Boot if I am trying to run the application without API ID it is throwing 404 error.

Here is the piece of code that I have written.

@RequestMapping(method = RequestMethod.GET, value = "/exchangerates/rates/{table}/{code}")
public @ResponseBody Object getAllCurriencyExchangeRates(@PathVariable String table, @PathVariable String code) {

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();

    ResponseEntity<Object> response = 
            restTemplate.getForEntity("http://api.nbp.pl/api/" +table+ "," +code+ Object.class, null, headers);

    return response;
}        

Actual query http://api.nbp.pl/api/exchangerates/rates/a/chf/

So, my question is can we call an external API without API ID?

Vivek Dharek
  • 93
  • 1
  • 1
  • 9
  • Are you sure you concatenate your String in a correct way? Why `+ Object.class` ? This returns "class java.lang.Object". – flohall Dec 08 '19 at 10:24
  • @flohall What could be best solution for that? – Vivek Dharek Dec 08 '19 at 10:28
  • first of all make sure that your URL is correct before worrying about any API ID. Just use `System.out.println()` to print out the url String you concatenated. I understand API ID in this case as a key or identifier which is used to authenticate. For sure their are external APIs in the world that doesn't need any authentication. This one for example doesn't need authentication. – flohall Dec 08 '19 at 10:36
  • Thanks for the solution, I tried to print but it is not printing anything on console. – Vivek Dharek Dec 08 '19 at 10:49

3 Answers3

3

First things first, you are trying to reach wrong API. That is why you are getting 404 not found. 404 means there is no url like you are calling.

Check your restTemplate carefully,

restTemplate.getForEntity("http://api.nbp.pl/api/" + table+ "," +code+ Object.class, null, headers);

You are doing wrong when concatenate strings. It should look something like this;

restTemplate.getForEntity("http://api.nbp.pl/api/exchangerates/rates/"+table+"/"+code, Object.class, null, headers);

And a hint for API developers, firstly you should play with api using Postman and then write code with api.

fuat
  • 1,484
  • 2
  • 19
  • 25
0

Try this - I have tested it - it works. Please keep in mind this is just a test implementation. Things inside main method have to be copied into your getAllCurriencyExchangeRates method. And for sure replace "a" and "chf" through variables. I assume table and code are the variables you want to use. I used String because I don't know which type of object you want to return. You can use your own pojo for sure instead of String.

package scripts;

import java.net.URI;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * author: flohall
 * date: 08.12.19
 */
public class Test {

    public static void main(final String[] args){
        final String url = "http://api.nbp.pl/api/exchangerates/rates";
        final URI uri = UriComponentsBuilder.fromHttpUrl(url).path("/").path("a").path("/").path("chf").build().toUri();
        System.out.println(uri);

        final RestOperations restTemplate = new RestTemplate();
        final ResponseEntity<String> result = restTemplate.getForEntity(uri, String.class);

        System.out.println(result.getBody());
    }

}
flohall
  • 967
  • 10
  • 19
0

Try with this

ResponseEntity<Object> response =
            restTemplate.getForEntity("http://api.nbp.pl/api/exchangerates/rates/" + table + "/" + code, Object.class, headers);
Sagar Ahuja
  • 637
  • 10
  • 10