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 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
}];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];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];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;