Introducing HTTP Client for server-side code

Backbeam let's you focus on your mobile apps without writing a single line of code in the backend, but you can also create web apps or you can build mobile solutions with server-side logic. So you can write server-side code or not. It is your choice.

In these days when you write server-side code you will probably need to integrate your backend with third-party services. Do you want to send SMS messages or automated voice calls? You can use Twilio or Tropo. Do you need an easy way to transform HTML files to PDF documents? You can use Docraptor for example. Do you want to extract the important parts of a web page such as its title, description and its most relevant image? You can use a service like Diffbot, etc.

All these services have their APIs and you will probably want to call them from your backend code and pick, transform and store the relevant information you need from them. So that's why you need to make HTTP requests to external services in your backend code and that's why we are introducing the HTTP Client feature!!

We want to show you a quick example that uses the StackOverflow API to fetch and show the users with the highest punctuations. We are going to create a simple web controller to fetch the data from stackoverflow and a simple HTML tempalate to show the results. For further information about this new API check out the HTTP Client documentation

Create a web controller that responds to, for example, GET / with the following code:

var http = backbeam.httpClient()
var url = 'http://api.stackexchange.com/2.1/users'
var qs = { site: 'stackoverflow', sort: 'reputation', order: 'desc' }
http.get({ url:url, qs:qs }, function(err, res) {
    var data = JSON.parse(res.body)
    var items = data.items
    response.render('index.html', { items: items }, true)
})

Now lets create a simple HTML template to show the data. The controller is passing a items array we will iterate. This is the code:

<table style="width:500px; margin: 0 auto; font-family: Lucida Grande, Helvetica, sans">
    <tr>
        <th>Name</th>
        <th>Gold</th>
        <th>Silver</th>
        <th>Bronze</th>
    </tr>
    {% for item in items %}
        <tr>
            <td><strong>{{ item.display_name }}</strong></td>
            <td>{{ item.badge_counts.gold }}</td>
            <td>{{ item.badge_counts.silver }}</td>
            <td>{{ item.badge_counts.bronze }}</td>
        </tr>
    {% endfor %}
</table>

So that's all! If you run this code you will see the most relevant people in StackOverflow with the number of medalls the have earned answering technical questions. And now you can integrate your backend with thousands of APIs out there :)