0
public class Spoonacular {
    private static final String SPOONACULAR_API= 'https://api.spoonacular.com';
    private static final String API_KEY= 'XXXXX';
    
    public static void getRandomReceipe(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(SPOONACULAR_API + '/recipes/random?apiKey=' + API_KEY);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        system.debug('response code'+ response.getStatusCode());
        if(response.getStatusCode() == 200) {
            system.debug('response'+ response.getbody());
        }
    }

}

Above mentioned code is giving me unexpected token '('error and the same error keeps popping up multiple times when I am working on different codes, even when I copy a code from trailhead module and execute it the same error pops up. Can someone please help me with the issue.

2
  • 2
    You just compromised your API key by posting it here. Go change it immediately or risk bots using your account.
    – David Reed
    Aug 17, 2021 at 6:01
  • @David Reed thankyou , I generated a new one. Do you have some fix for this issue? Aug 17, 2021 at 6:13

1 Answer 1

0

If the error is coming from the code you post, then it is likely to be caused by the API_KEY containing characters that should not be present in a URL.

One way to fix that is:

String encodedApiKey = EncodingUtil.urlEncode(API_KEY,'UTF-8');
request.setEndpoint(SPOONACULAR_API + '/recipes/random?apiKey=' + encodedApiKey);
4
  • Hi. Still getting the same error. Aug 17, 2021 at 7:51
  • Hi @SuchiTiwari, OK. Look for line number and class names in the error report and do turn on debug logging if you haven't already. Clarify if it is a compile-time or run-time error in your question too.
    – Keith C
    Aug 17, 2021 at 8:14
  • I checked the debug logs but could not find any debug log there. Can you help me how can I generate one? Aug 17, 2021 at 16:39
  • @SuchiTiwari By default debug logging is turned off. To turn on see e.g. salesforcegeneral.com/salesforcecom-debug-logs.
    – Keith C
    Aug 18, 2021 at 7:53

You must log in to answer this question.

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