Skip to content

Instantly share code, notes, and snippets.

@subnetmarco
Last active December 14, 2015 19:49
Show Gist options
  • Select an option

  • Save subnetmarco/5139686 to your computer and use it in GitHub Desktop.

Select an option

Save subnetmarco/5139686 to your computer and use it in GitHub Desktop.
unicorn-ios

Making a request

NSDictionary* headers = [NSDictionary dictionaryWithObjectsAndKeys:@"application/json", @"accept", nil];
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"param1", @"value2", @"param2", nil];
    
    HttpJsonResponse* response = [[Unicorn post:^(MultipartRequest* request) {
        [request setUrl:@"http://httpbin.org/post"];
        [request setHeaders:headers];
        [request setParameters:parameters];
    }] asJson];
    
    int code = [response code]; // The HTTP status code
    NSDictionary* responseHeaders = [response headers]; // The HTTP headers
    JsonNode* body = [response body]; // The parsed response
    NSData* rawBody = [response rawBody]; // The unparsed response

Making an async request

    NSDictionary* headers = [NSDictionary dictionaryWithObjectsAndKeys:@"application/json", @"accept", nil];
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"param1", @"value2", @"param2", nil];
    
    [[Unicorn post:^(MultipartRequest* request) {
        [request setUrl:@"http://httpbin.org/post"];
        [request setHeaders:headers];
        [request setParameters:parameters];
    }] asJsonAsync:^(HttpJsonResponse* response) {
        
        // This is the asyncronous callback block
        
        int code = [response code]; // The HTTP status code
        NSDictionary* responseHeaders = [response headers]; // The HTTP headers
        JsonNode* body = [response body]; // The parsed response
        NSData* rawBody = [response rawBody]; // The unparsed response
    }];

Multipart

Use a NSUrl object that points to the file.

 NSDictionary* headers = [NSDictionary dictionaryWithObjectsAndKeys:@"application/json", @"accept", nil];
    NSURL file = nil;
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"param1", file, @"param2", nil];
    
    HttpJsonResponse* response = [[Unicorn post:^(MultipartRequest* request) {
        [request setUrl:@"http://httpbin.org/post"];
        [request setHeaders:headers];
        [request setParameters:parameters];
    }] asJson];

Custom body

NSDictionary* headers = [NSDictionary dictionaryWithObjectsAndKeys:@"application/json", @"accept", nil];
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"param1", @"value2", @"param2", nil];
    
    HttpJsonResponse* response = [[Unicorn postEntity:^(BodyRequest* request) {
        [request setUrl:@"http://httpbin.org/post"];
        [request setHeaders:headers];
        // Converting NSDictionary to JSON:
        [request setBody:[NSJSONSerialization dataWithJSONObject:headers options:0 error:nil]];
    }] asJson];

Reference

Unicorn-ios uses configuration blocks of type SimpleRequest, MultipartRequest and BodyRequest to configure the URL, headers, parameters/body of the request.

+(HttpRequest*) get:(void (^)(SimpleRequest*)) config;

+(HttpRequestWithBody*) post:(void (^)(MultipartRequest*)) config;
+(HttpRequestWithBody*) postEntity:(void (^)(BodyRequest*)) config;

+(HttpRequestWithBody*) put:(void (^)(MultipartRequest*)) config;
+(HttpRequestWithBody*) putEntity:(void (^)(BodyRequest*)) config;

+(HttpRequestWithBody*) patch:(void (^)(MultipartRequest*)) config;
+(HttpRequestWithBody*) patchEntity:(void (^)(BodyRequest*)) config;

+(HttpRequestWithBody*) delete:(void (^)(MultipartRequest*)) config;
+(HttpRequestWithBody*) deleteEntity:(void (^)(BodyRequest*)) config;

The HttpRequest and HttpRequestWithBody can then be executed by calling one of:

-(HttpStringResponse*) asString;
-(void) asStringAsync:(void (^)(HttpStringResponse*)) response;

-(HttpBinaryResponse*) asBinary;
-(void) asBinaryAsync:(void (^)(HttpBinaryResponse*)) response;

-(HttpJsonResponse*) asJson;
-(void) asJsonAsync:(void (^)(HttpJsonResponse*)) response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment