-
Notifications
You must be signed in to change notification settings - Fork 21
Overview
BBHTTP was developed to help you get HTTP requests done as fast and effortlessly as possible.
By using blocks, ARC and some of the features introduced by Clang 3.1, it features a modern-feeling, clean and concise API that'll reduce your HTTP boilerplate down to a few lines.
NSURLConnection has a rather cumbersome and outdated API, not to mention the fact that it has a long standing annoying bug where if an upload is rejected by the server, it keeps sending data and eventually fails reporting time out or connection failure, instead of handing you back the error response the server sent.
Unlike other Objective-C HTTP frameworks, BBHTTP is built on top of libcurl which is actively maintained and used by millions around the world.
The most common use case these days seems to be retrieving JSON. Here's how you'd do it:
[[[BBHTTPRequest readResource:@"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:^(NSError* error) {
NSLog(@"Something bombed: %u %@", [error code], [error localizedDescription]);
}];
The JSON object will be available at the content
property of the response. You'll also notice that the keyed subscript operator is being used as it were valueForKeyPath:
instead of valueForKey:
. That's because the JSON response handler wraps the toplevel NSDictionary
(if the response yields one) in a BBJSONDictionary
, which pretty much just switches the functionality of objectForKeyedSubscript:
to call valueForKeyPath:
instead of valueForKey:
.
Please remember that this swap only occurs at the top-level dictionary so it's not available at sub-level dictionaries, i.e. you can't do this:
NSDictionary* firstUser = r.content[@"user_list"][0];
NSString* email = firstUser[@"account.email"]; // this would be looking for a property named "account.email"
BBHTTP can automatically convert various types of response content for you like images and text. Be sure to read the examples and response content handling pages for more information.