Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yayuhh/YYHRequest-Swift
Simple asynchronous HTTP networking class for Swift
https://github.com/yayuhh/YYHRequest-Swift
Last synced: 2 months ago
JSON representation
Simple asynchronous HTTP networking class for Swift
- Host: GitHub
- URL: https://github.com/yayuhh/YYHRequest-Swift
- Owner: yayuhh
- License: mit
- Created: 2014-06-04T04:18:34.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-02-12T17:06:30.000Z (almost 10 years ago)
- Last Synced: 2024-07-31T19:01:56.093Z (5 months ago)
- Language: Swift
- Homepage:
- Size: 290 KB
- Stars: 77
- Watchers: 8
- Forks: 15
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swift-cn - YYHRequest-Swift - http request in async. (Libs / Network)
README
YYHRequest
==========`YYHRequest` is a simple and lightweight class for loading asynchronous HTTP requests in Swift. 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 dataOriginal Objective-C version [available here](https://github.com/yayuhh/YYHRequest).
## Getting Started
Create and load a request
let request = YYHRequest(url: NSURL(string: "http://www.google.com/"))
request.loadWithCompletion {response, data, error in
if let actualError = error {
// handle error
} else if let actualResponse = response {
// handle success
}
}Create request and load manually.
let request = YYHRequest(url: NSURL(string: "http://www.google.com/"))
request.method = "POST"
request.parameters["foo"] = "bar"request.completionHandler = { response, data, error in
// request complete!
}request.loadRequest()
## Usage
### Load a Request
let request = YYHRequest(url: NSURL(string: "http://www.google.com/"))
request.loadWithCompletion { response, data, error in
// request complete!
}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
let request = YYHRequest(url: NSURL(string: "http://www.google.com/"))
request.parameters["foo"] = "bar"HTTP
GET /?foo=bar
### Posting Data
let request = YYHRequest(url: NSURL(string: "http://www.google.com/"))
request.method = "POST"
request.parameters["foo"] = "bar"HTTP
POST /
Content-Type: application/x-www-form-urlencoded
foo=bar