restsalesforceapexkobotoolbox

How to Fix Salesforce HTTP Send URL Issue


When I send this endpoint URL from Postman and Salesforce Developer Console, it worked from both interface: 'https://eu.kobotoolbox.org/api/v2/assets/aMeaT8CnGtoTTmooz3uKkq/data/?format=json'

But when I send this other query, it only worked from Postman, but failed in Salesforce Developer Console:

https://eu.kobotoolbox.org/api/v2/assets/aMeaT8CnGtoTTmooz3uKkq/data/?format=json&query={"_submission_time": {"$gt":"2023-11-16T01:02:03"}}&limit=3

From the Salesforce Developer's Console, it failed with Response Code 404.

Is there something else I have to do in Salesforce to make it work? Please help me.

SALESFORCE APEX CODE

HttpRequest req = new HttpRequest();
 String endpoint = 'https://eu.kobotoolbox.org/api/v2/assets/'+ koboproject + '/data/?format=json';
 endpoint += '&query={\"_submission_time\": {\"$gt\":\"' + LastDate + '\"}}';
 req.setEndpoint(endpoint);  
 system.debug(endpoint);
 req.setMethod('GET');
 req.setHeader('Authorization', 'Bearer b4c6cdf15eb891ab2a29495a02355d98925f9b2d');
 req.setHeader('Accept', 'application/json');
        
 Http http = new Http();
 HttpResponse resp = http.send(req);
 system.debug('RESP CODE:' + resp.getStatusCode());
 if (resp.getStatusCode() == 200) {
      return resp.getbody();
 else {
     return 'failed!';
 }

Solution

  • You can try encoding your query parameter using EncodingUtil.urlEncode(), salesforce enforces strict URL encoding

    HttpRequest req = new HttpRequest();
    String endpoint = 'https://eu.kobotoolbox.org/api/v2/assets/' + koboproject + '/data/?format=json';
    String query = '{"_submission_time": {"$gt":"' + LastDate + '"}}';
    endpoint += '&query=' + EncodingUtil.urlEncode(query, 'UTF-8');
    req.setEndpoint(endpoint);  
    system.debug(endpoint);