0
public class CurrencyConvertorClass {
    public static Decimal convertCurrencyToINR(Decimal eur){
        
        Decimal inr = 0.0;
        HTTP h = new HTTP();
        HTTPRequest req = new HTTPRequest();
        req.setEndpoint('https://exchangeratesapi.io');
        req.setMethod('GET');
        req.setHeader('Accept','application/json');
        HTTPResponse res = h.send(req);
        Map<String, Object> jsonBody = (Map<String,Object>)Json.deserializeUntyped(res.getBody());
        System.debug(jsonBody);
        Map<String,Object> mMap = (Map<String,Object>) JsonBody.get('rates');
        Decimal conversionRate = (Decimal) mMap.get('INR');
        inr = eur * conversionRate;
        return inr;
        
    }
}

2 Answers 2

4

Something went wrong with the web service call and you likely received some HTTP 400 or 500 error. Which starts with a <.

You should System.debug(res.getBody()) to see what exactly the service returned.

2

https://exchangeratesapi.io/ is the site home page, not the endpoint to call, so you're retrieving the HTML page, that's why the unexpected '<'.

In order to use Exchange Rates API you must have an API Key, you can register a free account.

Once you got you API Key, you can call the following endpoint https://api.exchangeratesapi.io/v1/latest passing your API Key as query parameter access_key=YOUR_API_KEY.
You could add both a base currency iso code and a list of currency code to limit the output as query parameters, i.e. base=EUR and symbols=INR, so , so the full url will be https://api.exchangeratesapi.io/v1/latest?access_key=YOUR_API_KEY&base=EUR&symbols=INR. The ouput will be like:

{
    "success": true,
    "timestamp": 123456789,
    "base": "EUR",
    "date": "2021-07-27",
    "rates": {
        "INR": 87.79
    }
}

After sending the request, you should check the response status code res.getstatusCode(). If everything went fine it should be 200. Only in this case you can parse the response body.
Please consider defining an appropriate Apex class to map that output in order to get advantage of typed deserialization.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .