Skip to content

Simple asynchronous networking class built on NSURLConnection and NSOperationQueue

License

Notifications You must be signed in to change notification settings

yayuhh/YYHRequest

Repository files navigation

YYHRequest

Build Status Stories in Ready

Simple and lightweight class for loading asynchronous HTTP requests. Built on NSURLConnection and NSOperationQueue. YYHRequest is not intended to be a full-featured networking framework but instead a simple wrapper to avoid the boilerplate of using NSURLConnection and NSURLRequest for simple networking tasks.

  • Lightweight design - just a single wrapper class
  • Avoid the boilerplate of NSURLConnection and NSURLRequest for simple networking tasks
  • Simple API for setting request headers, query parameters, and form data
  • Block-based success and failure callbacks for processing response data

Installation

Install with CocoaPods.

pod 'YYHRequest'

Install JSON support

pod 'YYHRequest/JSON'

Getting Started

Create and load a request

NSURL *url = [NSURL URLWithString:@"http://foo.bar"];

[YYHRequest loadRequestWithURL:url success:^(NSData *data) {
    // request complete
} failure:^(NSError *error) {
    // request failed
}];

Create request and load manually.

YYHRequest *request = [YYHRequest requestWithURL:url];
request.method = @"POST";
request.headers[@"Accept"] = @"application/json"
request.parameters[@"name"] = @"value";

[request onSuccess:^(NSData *data) {
    // request complete
}];

[request loadRequest];

Usage

Load a Request

NSURL *url = [NSURL URLWithString:@"/"];

[YYHRequest loadRequestWithURL:url success:^(NSData *data) {
    // request complete
} failure:^(NSError *error) {
    // request failed
}];

HTTP

GET /

Customize a Request

// set request method
request.method = @"PUT";

// set HTTP headers using headers dictionary
request.headers[@"User-Agent"] = @"value";

// set header values via properties
request.userAgent = @"value";

HTTP

PUT /
User-Agent: value
Content-Type: application/x-www-form-urlencoded

Sending Query Parameters

YYHRequest *request = [YYHRequest requestWithURL:[NSURL URLWithString:@"/"]];
request.parameters[@"foo"] = @"bar";

HTTP

GET /?foo=bar

Posting Data

YYHRequest *request = [YYHRequest requestWithURL:[NSURL URLWithString:@"/"]];
request.method = @"POST";
request.parameters[@"foo"] = @"bar";

HTTP

POST /
Content-Type: application/x-www-form-urlencoded
foo=bar

Posting JSON

YYHJSONRequest *request = [YYHJSONRequest requestWithURL:[NSURL URLWithString:@"/"]];
request.method = @"POST";
request.parameters[@"foo"] = @"bar";

HTTP

POST /
Content-Type: application/json
{"foo":"bar"}

Documentation

See the documentation for more information.

Changes

Change log

About

Simple asynchronous networking class built on NSURLConnection and NSOperationQueue

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •