Coder Social home page Coder Social logo

donjordano / unirest-obj-c Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kong/unirest-obj-c

0.0 1.0 0.0 681 KB

Unirest in Objective-C: Simplified, lightweight HTTP library.

Home Page: http://unirest.io/objective-c

License: MIT License

unirest-obj-c's Introduction

Unirest for Objective-C Build Status

Unirest is a set of lightweight HTTP libraries available in multiple languages, ideal for most applications:

  • Make GET, POST, PUT, PATCH, DELETE requests
  • Both syncronous and asynchronous (non-blocking) requests
  • It supports form parameters, file uploads and custom body entities
  • Supports gzip
  • Supports Basic Authentication natively
  • Customizable timeout
  • Customizable default headers for every request (DRY)
  • Automatic JSON parsing into native object (NSDictionary or NSArray) for JSON responses

Created with love by thefosk @ mashape.com

Installing

Download the Objective-C Unirest Library from GitHub (or clone the repo) and import the folder into your project. You can also install Unirest-obj-c with CocoaPods.

CocoaPods

If you decide to use CocoaPods, create a Podfile file in your project's folder:

$ edit Podfile
platform :ios, '5.0'
pod 'Unirest', '~> 1.1.2'

and then execute pod install. Make sure to always open the Xcode workspace instead of the project file when building your project:

$ open App.xcworkspace

Now you can import your dependencies:

#import <UNIRest.h>

Requirements

The Unirest-Obj-C client library requires ARC (Automatic Reference Counting) to be enabled in your Xcode project. To enable ARC select your project or target and then go to Build Settings and under the section Apple LLVM compiler 3.0 - Language you will see the option Objective-C Automatic Reference Counting:

Enable ARC in Xcode

For existing projects, fortunately Xcode offers a tool to convert existing code to ARC, which is available at Edit -> Refactor -> Convert to Objective-C ARC

Creating Request

So you're probably wondering how using Unirest makes creating requests in Objective-C easier, let's look at a working example:

NSDictionary* headers = @{@"accept": @"application/json"};
NSDictionary* parameters = @{@"parameter": @"value", @"foo": @"bar"};

UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
  [request setUrl:@"http://httpbin.org/post"];
  [request setHeaders:headers];
  [request setParameters:parameters];
}] asJson];

Just like in the Unirest Java library the Objective-C library supports multiple response types given as the last parameter. In the example above we use asJson to get a JSON response, likewise there are asBinary and asString for responses of other nature such as file data and hypermedia responses.

Asynchronous Requests

For non-blocking requests you will want to make an asychronous request to keep your application going while data is fetched or updated in the background, doing so with unirest is extremely easy with barely any code change from the previous example:

NSDictionary* headers = @{@"accept": @"application/json"};
NSDictionary* parameters = @{@"parameter": @"value", @"foo": @"bar"};

[[UNIRest post:^(UNISimpleRequest* request) {
  [request setUrl:@"http://httpbin.org/post"];
  [request setHeaders:headers];
  [request setParameters:parameters];
}] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {
  // This is the asyncronous callback block
  NSInteger* code = [response code];
  NSDictionary* responseHeaders = [response headers];
  UNIJsonNode* body = [response body];
  NSData* rawBody = [response rawBody];
}];

Cancel Asynchronous Request

You can cancel an asyncronous request by invoking the cancel method on the UNIUrlConnection object:

UNIUrlConnection* asyncConnection = [[UNIRest get:^(UNISimpleRequest *simpleRequest) {
    [request setUrl:@"http://httpbin.org/get"];
}] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
    // Do something
}];

[asyncConnection cancel]; // Cancel request

File Uploads

Transferring files through request with unirest in Objective-C can be done by creating a NSURL object and passing it along as a parameter value with a UNISimpleRequest like so:

NSDictionary* headers = @{@"accept": @"application/json"};
NSURL* file = nil;
NSDictionary* parameters = @{@"parameter": @"value", @"file": file};

UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
  [request setUrl:@"http://httpbin.org/post"];
  [request setHeaders:headers];
  [request setParameters:parameters];
}] asJson];

Custom Entity Body

To send a custom body such as JSON simply serialize your data utilizing the NSJSONSerialization with a BodyRequest and [method]Entity instead of just [method] block like so:

NSDictionary* headers = @{@"accept": @"application/json"};
NSDictionary* parameters = @{@"parameter": @"value", @"foo": @"bar"};

UNIHTTPJsonResponse* response = [[UNIRest postEntity:^(UNIBodyRequest* request) {
  [request setUrl:@"http://httpbin.org/post"];
  [request setHeaders:headers];
  // Converting NSDictionary to JSON:
  [request setBody:[NSJSONSerialization dataWithJSONObject:headers options:0 error:nil]];
}] asJson];

Basic Authentication

Authenticating the request with basic authentication can be done by setting the username and password properties in the builder:

UNIHTTPJsonResponse* response = [[UNIRest get:^(UNISimpleRequest * request) {
    [request setUrl:@"http://httpbin.org/get"];
    [request setUsername:@"user"];
    [request setPassword:@"password"];
}] asJson];

Request

The Objective-C unirest library uses configuration blocks of type UNISimpleRequest and UNIBodyRequest to configure the URL, Headers, and Parameters / Body of the request.

+(UNIHTTPRequest*) get:(void (^)(UNISimpleRequestBlock*)) config;

+(UNIHTTPRequestWithBody*) post:(void (^)(UNISimpleRequestBlock*)) config;
+(UNIHTTPRequestWithBody*) postEntity:(void (^)(UNIBodyRequestBlock*)) config;

+(UNIHTTPRequestWithBody*) put:(void (^)(UNISimpleRequestBlock*)) config;
+(UNIHTTPRequestWithBody*) putEntity:(void (^)(UNIBodyRequestBlock*)) config;

+(UNIHTTPRequestWithBody*) patch:(void (^)(UNISimpleRequestBlock*)) config;
+(UNIHTTPRequestWithBody*) patchEntity:(void (^)(UNIBodyRequestBlock*)) config;

+(UNIHTTPRequestWithBody*) delete:(void (^)(UNISimpleRequestBlock*)) config;
+(UNIHTTPRequestWithBody*) deleteEntity:(void (^)(UNIBodyRequestBlock*)) config;
  • UNIHTTPRequest [UNIRest get: (void (^)(UNISimpleRequestBlock*))] config;

    Sends equivalent request with method type to given URL

  • UNIHTTPRequestWithBody [UNIRest (post|postEntity): (void (^)(UNISimpleRequestBlock|UNIBodyRequestBlock)(*))] config;

    Sends equivalent request with method type to given URL

  • UNIHTTPRequestWithBody [UNIRest (put|putEntity): (void (^)(UNISimpleRequestBlock|UNIBodyRequestBlock)(*))] config;

    Sends equivalent request with method type to given URL

  • UNIHTTPRequestWithBody [UNIRest (patch|patchEntity): (void (^)(UNISimpleRequestBlock|UNIBodyRequestBlock)(*))] config;

    Sends equivalent request with method type to given URL

  • UNIHTTPRequestWithBody [UNIRest (delete|deleteEntity): (void (^)(UNISimpleRequestBlock|UNIBodyRequestBlock)(*))] config;

    Sends equivalent request with method type to given URL

Response

The UNIHTTPRequest and UNIHTTPRequestWithBody can then be executed by calling one of:

-(UNIHTTPStringResponse*) asString;
-(UNIHTTPStringResponse*) asString:(NSError**) error;
-(UNIUrlConnection*) asStringAsync:(UNIHTTPStringResponseBlock) response;

-(UNIHTTPBinaryResponse*) asBinary;
-(UNIHTTPBinaryResponse*) asBinary:(NSError**) error;
-(UNIUrlConnection*) asBinaryAsync:(UNIHTTPBinaryResponseBlock) response;

-(UNIHTTPJsonResponse*) asJson;
-(UNIHTTPJsonResponse*) asJson:(NSError**) error;
-(UNIUrlConnection*) asJsonAsync:(UNIHTTPJsonResponseBlock) response;
  • -(UNIHTTPStringResponse*) asString;

    Blocking request call with response returned as string for Hypermedia APIs or other.

  • -(UNIHTTPStringResponse*) asString:(NSError**) error;

    Blocking request call with response returned as string and error handling.

  • -(UNIUrlConnection*) asStringAsync:(UNIHTTPStringResponseBlock) response;

    Asynchronous request call with response returned as string for Hypermedia APIs or other.

  • -(UNIHTTPBinaryResponse*) asBinary;

    Blocking request call with response returned as binary output for files and other media.

  • -(UNIHTTPBinaryResponse*) asBinary:(NSError**) error;

    Blocking request call with response returned as binary output and error handling.

  • -(UNIUrlConnection*) asBinaryAsync:(UNIHTTPBinaryResponseBlock) response;

    Asynchronous request call with response returned as binary output for files and other media.

  • -(UNIHTTPJsonResponse*) asJson;

    Blocking request call with response returned as JSON.

  • -(UNIHTTPJsonResponse*) asString:(NSError**) error;

    Blocking request call with response returned as JSON and error handling.

  • -(UNIUrlConnection*) asJsonAsync:(UNIHTTPJsonResponseBlock) response;

    Asynchronous request call with response returned as JSON.

Advanced Configuration

You can set some advanced configuration to tune Unirest-Obj-C:

Timeout

You can set a custom timeout value (in seconds):

[UNIRest timeout:2];

By default the timeout is 60.

Default Request Headers

You can set default headers that will be sent on every request:

[UNIRest defaultHeader:@"Header1" value:@"Value1"];
[UNIRest defaultHeader:@"Header2" value:@"Value2"];

You can clear the default headers anytime with:

[UNIRest clearDefaultHeaders];

unirest-obj-c's People

Contributors

subnetmarco avatar nijikokun avatar seivan avatar zoltan-tornoczky avatar esseguin avatar sonicaghi avatar connorduggan avatar coryalder avatar neilkimmett avatar

Watchers

Ivan Yordanov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.