4

My requirement is to call an external REST based service from a trigger. This service has been implemented with the GET method. I have finished the code for the trigger and the class.

Now I am creating test classes and a mock in place of the callout. The mock needs to send two separate responses. A success response or a failure response. Here is the mock I am currently writing.

global class RestCalloutMockImpl implements HttpCalloutMock
{
    global HTTPResponse respond(HTTPRequest req) 
    {
        HttpResponse res = new HttpResponse();
        System.Debug('Endpoint contains '+ req.getEndpoint()); //No query string parameters here.
        System.Debug('Method contains '+ req.getMethod()); //GET
        System.Debug('Body contains ' + req.getBody)); //NULL
        res.setHeader('Content-Type','application/json');
        //PSEUDOCODE follows. This is where I want to read the query string paramter values.
        if(check for parameter value here)
        {
            res.setBody('{"ErrorMessage":null,"ResultValue":"Success"}');
            res.setStatusCode(200);
        }
        else
        {   //Simulate Failure here.
            res.setBody('{"ErrorMessage":"Life is Short!","ResultValue":"Failure"}');
            res.setStatusCode(200);            
        }       
        return res;

    }
}

I have been through the documentation on the HttpRequest class. I could not find any method that will output the parameters sent on the query string.

If the calling method is a POST, the getBody() method can be used to see read the contents.

I have tried to use the RestContext class, but the value for that is NULL.

1

You can use the PageReference class to read the query string, if any, from the URL:

PageReference ref = new PageReference(req.getEndpoint());
System.debug(ref.getParameters());

If the class performing the callout does not specify a query string, then ref.getParameters() will contain no values.

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.