Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Examples

brunodecarvalho edited this page Feb 7, 2013 · 3 revisions

Examples

This page contains examples for some of the most common use cases.

Get JSON

[[[BBHTTPRequest getResource:@"http://foo.bar/baz.json"] asJSON] execute:^(BBHTTPResponse* r) {
    NSLog(@"User email: %@", r.content[@"user.email"]);
    NSLog(@"# of followers: %@", r.content[@"user.followers.@count"]);
} error:nil];

Download images

[[BBHTTPRequest getResource:@"http://foo.bar/baz.png"] asImage] execute:^(BBHTTPResponse* response) {
    UIImage* image = response.content; // NSImage on OSX
    NSLog(@"image size: %@", NSStringFromCGSize(image.size));
} error:nil];

Convert response body to string

[[[BBHTTPRequest getResource:@"http://foo.bar/baz.json"] asString] execute:^(BBHTTPResponse* r) {
    NSLog(@"%@", r.content);
} error:nil];

Upload files

[[BBHTTPRequest createResource:@"http://foo.bar/baz" withContentsOfFile:@"/path/to/file"]
 execute:^(BBHTTPResponse* response) {
     // handle response
 } error:nil];

Download files

[[BBHTTPRequest getResource:@"http://foo.bar/baz"] setup:^(BBHTTPRequest* request) {
    [request downloadToFile:@"/path/to/file"];
} execute:^(BBHTTPResponse* response) {
    // handle response
} error:nil];

Prepare, execute and cleanup

[[BBHTTPRequest getResource:@"http://foo.bar/baz/1"] setup:^(id request) {
    // Prepare request...
} execute:^(BBHTTPResponse* response) {
    // Handle response...
} error:^(NSError* error) {
    // Handle error...
} finally:^{
    // Do after error OR success.
}];

Lower level API

All requests are executed by a BBHTTPExecutor. If you don't specify one, these requests go on the singleton executor. If you want to control exactly how your requests behave, there's a lower level API for that too:

BBHTTPExecutor* twitterExecutor = [BBHTTPExecutor initWithId:@"twitter.com"];
BBHTTPExecutor* facebookExecutor = [BBHTTPExecutor initWithId:@"facebook.com"];
twitterExecutor.maxParallelRequests = 10;
facebookExecutor.maxParallelRequests = 2;
...
BBHTTPRequest* request = [[BBHTTPRequest alloc]
                          initWithURL:[NSURL URLWithString:@"http://twitter.com/resource"]
                          andVerb:@"GET"];

request[@"Accept-Language"] = @"en-us";
request.downloadProgressBlock = ^(NSUInteger current, NSUInteger total) { /* ... */ };
// The finish block will be called when the request ends successfully, with error or cancelled
request.finishBlock = ^(BBHTTPRequest* request) {
    if ([request wasCancelled]) {
        // Handle request cancellation, only happens when you call the 'cancel' method
    } else if ([request hasSuccessfulResponse]) {
        // Handle completed request with success response from server
    } else if ([request wasSuccessfullyExecuted]) {
        // Handle completed request with error response from server
    } else {
        // Some other error occurred which prevented the request from terminating
        // The 'error' read-only property will be set
    }
};

[twitterExecutor executeRequest:request];
Clone this wiki locally