Remote Sites + load balancing = a gotcha?

My favorite aspect of developing on the platform is how quickly you can get something up and running. Recently, I’ve been chewing on an idea for how to better the integrate the photos I share on Twitter into my personal blog on Tumblr, though it would require a little bit of coding. I had a little downtime today and got almost all the way there.


As I mentioned, I keep my personal blog on Tumblr. It’s a neat tool, and I’ve always liked the way that it can pull in content from other sources (usually via RSS) and aggregate everything you’re sharing online. I’m active on Twitter, so naturally I configured my tumblog to read in the RSS feed of my tweets. This works great for short text blurbs, which is the heart and soul of Twitter.

A picture is worth a thousand words, of course, and being an amateur photographer I often post quick snapshots on Twitter via yFrog, one of several services that extends Twitter to provide photo sharing. These tweets will include a shortened URL to the photo, which makes perfect sense in your Twitter feed but leaves room for improvement when that same tweet is copied onto a blog. Tumblr includes a native post type for photos, so my idea is to have a piece of code sweep through my Tumblr posts looking for links to “” and replace those posts with full-fledged photo posts.

As luck would have it, both Tumblr and yFrog offer REST APIs that allow this type of thing to be straightforward. The flow of the code is basically as follows:

  1. Read in recent Tumblr text posts (via the Tumblr API), and keep a list of those that include links to yFrog
  2. Run through that list and use the yFrog API to determine the URL to the full size image for each
  3. Create a Tumblr photo post for each that links to the URL from step 2 (rather than uploading an actual image)
  4. Delete the original Tumblr text posts that would then be a duplicate

Unfortunately, I ran into a problem at step 2.

The Issue requires any sites that your code will access via HTTP to be specifically whitelisted under Setup → Security Controls → Remote Site Settings. In the case of Tumblr, blog entries are created by sending an HTTP POST with various parameters to the following URL:

The screenshot below shows the Remote Site configuration used for that example:

Remote Site configuration
Remote Site configuration for Tumblr

Accessing the image metadata via the yFrog API is conceptually similar, except the HTTP GET request is redirected to a different URL (presumably for load balancing purposes). Here’s a screenshot from HTTP Client that shows the actual response:

Image API call redirected
HTTP response being redirected

The Location header provides the URL that now accesses this resource. I changed my Apex code to account for a possible redirect, but then I ran into a problem with the Remote Site:

Remote Site error with dynamic subdomain
Remote Site error with dynamic subdomain

The part of the URL underlined in red is the part that would generally need to be configured as the Remote Site URL. However, this isn’t practical, since each time the initial GET is performed, the redirected URL has a different subdomain. Again, this is understandable as a load balancing mechanism, but unfortunately puts the brakes on my mashup.

Possible solutions

I can appreciate that the Remote Site provides a layer of security, though perhaps a compromise would be to allow wildcards in this configuration, as in:


Alternatively, the platform’s native HTTP mechanism could automatically follow those redirects, similar to how this is handled in Google App Engine. That would eliminate the need for the Apex code to handle redirects.

As a workaround, I’ve started sharing images with a service called Twitgoo (instead of yFrog). They have an API as well, and calling the methods does not result in a redirect.


Here’s an example response:

<?xml version="1.0" encoding="UTF-8"?> 
<rsp status="ok"> 
   <text>Awesome Denver city lights</text> 

Crisis averted. 🙂