Playing with HTTP libraries

Date Arrow  March 26, 2008

It’s fun to find out how to tackle the same task in different programming languages; in this case, it’s all about doing HTTP requests over a network: fortunately, there are networking libraries in virtually all major programming languages. In my current project, I’m generating wrappers easing the access to the core of the project itself, a RESTful API. This way, developers interested in using the API can just take a wrapper, include it in their projects, and start coding right away. No need to know this (relatively low-level) stuff; just use the API. The wrappers themselves are auto-generated from the API definition itself, but that’s another story ;)

Below there is a sample of the different ways I’ve found to do a network access to a remote server, using HTTP Basic Authentication and a couple of headers, in PHP, Ruby, Python, JavaScript, and even Objective-C! I’m even generating ActionScript 3.0 code, but I’m not a Flash coder :) So I’ll post the wrappers that work best at the moment, and in the future I’ll include other examples, particularly for .NET, C++ and Java.

In all the cases below, there is a “request” function or method that takes an HTTP verb (GET, POST, PUT, DELETE, etc), a URL (without the slash “/” at the beginning) and some parameter data, in the form of a dictionary. The function wraps the underlying libraries of each programming language, offering a simpler interface, and allowing for HTTP Basic Authentication (for HTTP Digest Authentication it would be much, much more complex!). There are synchronous (useful for server or command-line applications) and asynchronous versions (for GUI systems). Off to the code!

In PHP (synchronous):

The curl library has a distinctive C smell :) The good thing is that after doing this wrapper, doing the C++ one will be fairly straightforward!

In Ruby (synchronous):

The Ruby library is the only one I found so far that features the four HTTP verbs in the interface!

In Python (synchronous):

I much prefer the Ruby way, if you ask me. I don’t know, it’s more elegant. I can’t get used to the indenting!

In JavaScript, using Prototype (asynchronous):

Again in JavaScript, but this time using jQuery (asynchronous):

The last JavaScript one, but this time using bare bones XMLHttpRequest (asynchronous):

To use these JavaScript versions, just pass a couple of functions as parameters, and you’re done; they will be called in case of success or error, asynchronously.

Finally, the Cocoa / Objective-C wrapper (both synchronous and asynchronous, header and implementation files):

Now the implementation file:

Rather verbose this last one huh? Objective-C has a distinctive characteristic, inherited from Smalltalk (I suppose): methods with named parameters, like “stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding” (decidedly, one of the longest method calls ever :) The good thing is that the code reads like a book. This is why I like this language, as well as Ruby: you can read code in these languages very easily.

Another nice aspect of this Objective-C wrapper is that if you have an API that can return “Property Lists” (like the one I’ve been working on lately), you can send object graphs serialized in XML that are completely Cocoa-compatible. That’s what the getPropertyList method does: client code can deal with standard NSDictionary instances, no need to do anything else!

Of course these are not the only ways to do this, but so far these wrappers have helped me a lot. Don’t hesitate to leave your comments below! And most important, have fun! As always, use this code at your own risk.

Tagged   Code · How to?

5 Comments

  • #1.   Yoan 03.27.2008

    httplib has AIDS in it. http://code.google.com/p/httplib2/

  • #2.   Chase 06.27.2008

    Could you post some sample code on implementing your Obj C sample codes? I am new to Obj C, and am frustrated with not being able to make a simple HTTP Get request… and be able to read what the returned XML. Thank you!

  • #3.   Adrian 06.27.2008

    Hi Chase,
    In the comment section of the ObjC wrapper above you can find a quick sample of how to use the class:

    #import
    int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool;
    pool = [[NSAutoreleasePool alloc] init];

    AKResourceSubClass* resource;
    resource = [[AKResourceSubClass alloc] init];
    [resource get];
    NSDictionary* plist = [resource getPropertyList];
    // Do something with the results…

    [pool drain];
    return 0;
    }

    Take into account that to read generic XML you should use the [resource getResponseText] message. This class works best (and is easier to use) if your XML follows the plist (property list) specs from Apple…

  • #4.   Adrian 06.27.2008

    I forgot to add that you should subclass the AKResource class, and the “get:” method should be implemented there, calling the sendRequestTo:usingVerb:withParameters: mother class method, with the required parameters, of course.

  • #5.   Chase 06.27.2008

    Adrian, wow, that was fast man! I know C# and AS3 really well, but I am not wrapping my brain around Obj C very fast. I tried for 2 hours last night to get your code to get a readable response, but it was a no go. Could you post some a simple implementation that subclasses AKResource and sends (via get) a few vars to a php file and shows the response?

Commenting