2

I have written a trigger that parses url's stored in a number of objects and requests them to see if they are still valid.

private Boolean pollserver(string url, string referer)
    {
      HttpRequest req = new HttpRequest(); 

      req.setEndpoint(url);
      req.setMethod('GET');
      req.setHeader('Referer', referer);

      HTTPResponse res;
      Http http = new Http();
      if(!test.isRunningTest())
      {
        res = http.send(req);   
      }
      else
      {
        //We're running as a test, lets set some mock data
        res = new HTTPresponse();
        if(url == this.ValidURL)
        {
            res.setStatusCode(200);
        }
        else
        {
            res.setStatusCode(404);
        }
      }


      system.debug(url);
      System.debug(res.getStatusCode());
      System.debug('');

      return isFound(res);            
    }

It works fine, except when the file being returned is larger than 3mb. Is there a good way to implement a 'ping' in Apex so I can see if a request completed without actually requesting the body? Thanks!

  • 1
    use a HEAD request rather than GET, that'll return all the headers for the URL, but not the actual body. – superfell May 19 '16 at 14:29
  • I can not believe that didn't occur to me already... Thanks! – Grisk May 19 '16 at 14:34
  • @super post that as an answer! – Adrian Larson May 19 '16 at 14:43
4

If you use a HEAD request rather than a GET, the server will return all the headers associated with the URL (and so you can tell if it exists, content type etc), but won't return the body.

so instead of req.setMethod('GET'); you'd do req.setMethod('HEAD');

| improve this answer | |
  • Might be worth just adding code to demonstrate where you set that. :) – Adrian Larson May 19 '16 at 16:31

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.