2

I have set up an apex class as a REST endpoint for an HTTP GET request that an external system is sending. We're having some trouble with it and they want me to send them the request detail. How do I get the full URL with the querystring appended so I can System.debug it and send it to them?

Also are there any other useful methods that return information I/they can use?

The REST class has one HTTPGet method:

@RestResource(urlMapping='/smscallback')
global class SMSCallbackController {
    @HttpGet
    global static String doGet() {

    System.debug(RestContext.request.requestBody);

    String g_transactionId = RestContext.request.params.get('tranid');
    String g_messageGuid = RestContext.request.params.get('messageguid');
    String g_recipient = RestContext.request.params.get('recipient');
    String g_statusId = RestContext.request.params.get('statusid');
    String g_datetime = RestContext.request.params.get('datetime');
    String g_clientRef = RestContext.request.params.get('clientref');

    System.debug('Transaction ID: ' + g_transactionId);
    System.debug('Message GUID: ' + g_messageGuid);
    System.debug('Recipient: ' + g_recipient);
    System.debug('Status: ' + g_statusId);
    System.debug('Date/Time: ' + g_datetime);
    System.debug('Security Ref: ' + g_clientRef);

    SMS_Message__c relatedRecord;

    //check record exists
    try {
        relatedRecord = [SELECT Transaction_ID__c, Recipient_Phone__c, Salt__c FROM SMS_Message__c WHERE Transaction_ID__c = :g_transactionId AND Recipient_Phone__c = :g_recipient LIMIT 1];
    } catch (Exception e) {
        System.debug(e);

        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json'); 
        String jsonResponse = '';
        res.statusCode = 500;
        jsonResponse = '{"response": {"status": "Failed", "message":"Server Error."}}';
        res.responseBody = blob.valueOf(jsonResponse);

        return 'No matching transaction found!';
    }

    //check hash
    g_clientref = EncodingUtil.urlDecode(g_clientref, 'UTF-8');

    String valueToHash = relatedRecord.Recipient_Phone__c + String.valueOf(relatedRecord.Salt__c);
    Blob b = Crypto.generateDigest('SHA-256', Blob.valueOf(valueToHash));

    if (g_clientref == EncodingUtil.base64Encode(b)) {
        //if hash matches, set delivery code and received time
        relatedRecord.Delivery_Status_Code__c = g_statusId;

        String dtStr = g_datetime + '+01:00';
        System.debug('Date/Time string to save:' + dtStr);
        Datetime dt = (Datetime)json.deserialize('"' + dtStr + '"', datetime.class);
        System.debug('Date/Time to save:' + String.valueOf(dt));
        System.debug('hour(): ' + dt.hour());
        System.debug('hourGmt(): ' + dt.hourGmt());
        relatedRecord.Receipt_Received_At__c = dt;

        relatedRecord.Message_GUID__c = g_messageGuid;
        update relatedRecord;

        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json'); 
        String jsonResponse = '';
        res.statusCode = 200;
        jsonResponse = '{"response": {"status": "Success", "message":"Received."}}';
        res.responseBody = blob.valueOf(jsonResponse);

        return 'Message details successfully updated!';
    } else {
        //hash does not match
        return 'Security does not match!';
    }              
}

}

  • 1
    Can we see your Rest apex class/method ? I don't think there's a feature to get that url out of the box. Shouldn't be to hard to compose yourself ? – Samuel De Rycke Jun 20 '14 at 9:20
  • Added as an edit :) – Adam Jun 20 '14 at 9:24
  • BTW - debugging apex REST can be facilitated by using SFDC Workbench and the REST Explorer option - you can paste in various rest resources with parameters and see how they work. e.g. as in @Samuel De Rycke's answer below: /services/apexrest/smscallback?tranid=xxxx&messageguid=yyyyy&recipient=zzzzzzz&....etc. – cropredy Jun 20 '14 at 17:57
6

The base url is quite clearly stated in the documentation :

Apex REST methods can be used in managed and unmanaged packages. When calling Apex REST methods that are contained in a managed package, you need to include the managed package namespace in the REST call URL. For example, if the class is contained in a managed package namespace called packageNamespace and the Apex REST methods use a URL mapping of /MyMethod/*, the URL used via REST to call these methods would be of the form https://instance.salesforce.com/services/apexrest/packageNamespace/MyMethod/

So in your case this would become:

https://yourinstance.salesforce.com/services/apexrest/smscallback

After that it's just a matter of appending your parameters with values that make sense to debug on ..

https://yourinstance.salesforce.com/services/apexrest/smscallback?tranid=xxxx&messageguid=yyyyy&recipient=zzzzzzz&....etc.
2
String restrequestURL = URL.getSalesforceBaseUrl().toExternalForm();
RestRequest req = RestContext.request;        
restrequestURL=restrequestURL+'/services/apexrest/'+RestContext.request.requestURI+'?';
for(String s:RestContext.request.params.keyset()){
    restrequestURL=restrequestURL+s+'='+RestContext.request.params.get(s)+'&';
}
restrequestURL=restrequestURL.removeEnd('&');
system.debug('*******'+restrequestURL);

The above is a very ugly way that i scratched to print request URL in a dynamic fashion if request is reaching at SFDC end .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.