Simple asynchronous HTTP networking class for Swift

Overview

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 data

Original Objective-C version available here.

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
You might also like...
Showcasing simple SwiftUI and networking capabilities

CovidCounts CovidCounts is powered by SwiftUI. It allows a user to view COVID related data for different U.S. States. App Purpose This app is showcasi

A simple class to check for internet connection availability in Swift.

Reach A simple class to check for internet connection availability in Swift. Works for both 3G and WiFi connections. Install Manually Add the Reach.sw

A simple GCD based HTTP client and server, written in 'pure' Swift
A simple GCD based HTTP client and server, written in 'pure' Swift

SwiftyHTTP Note: I'm probably not going to update this any further - If you need a Swift networking toolset for the server side, consider: Macro.swift

A simple HTTP server written in Swift

http4swift http4swift is a tiny HTTP server library for Nest-compatible applications. This project is unstable, and the API might be changed at anytim

Parsing Simple HTTP Headers for swift

HTTP Headers Parsing simple HTTP headers using pre-defined header descriptions. Examples: let response = HTTPURLRseponse(..., headers: [ "X-RateLi

A simple, lighweight library for making http requets in iOS

HttpKIT Super simple, super lighweight library for making http requets in IOS. It is a work in progress so PR's are definitelty welcome and highly enc

Robust Swift networking for web APIs
Robust Swift networking for web APIs

Conduit Conduit is a session-based Swift HTTP networking and auth library. Within each session, requests are sent through a serial pipeline before bei

A type-safe, high-level networking solution for Swift apps
A type-safe, high-level networking solution for Swift apps

What Type-safe network calls made easy Netswift offers an easy way to perform network calls in a structured and type-safe way. Why Networking in Swift

A Swift Multiplatform Single-threaded Non-blocking Web and Networking Framework
A Swift Multiplatform Single-threaded Non-blocking Web and Networking Framework

Serverside non-blocking IO in Swift Ask questions in our Slack channel! Lightning (formerly Edge) Node Lightning is an HTTP Server and TCP Client/Serv

Comments
Owner
yayuhh
yayuhh
QwikHttp is a robust, yet lightweight and simple to use HTTP networking library for iOS, tvOS and watchOS

QwikHttp is a robust, yet lightweight and simple to use HTTP networking library. It allows you to customize every aspect of your http requests within a single line of code, using a Builder style syntax to keep your code super clean.

Logan Sease 2 Mar 20, 2022
🤵🏽‍♀️ Janet — A thin HTTP networking layer built on URLSession for simple, declarative endpoint specification leveraging the power of async/await.

????‍♀️ Janet — Just another networking kit — A thin HTTP networking layer built on URLSession for simple, declarative endpoint specification leveragi

Niklas Holloh 3 Sep 6, 2022
Elegant HTTP Networking in Swift

Alamofire is an HTTP networking library written in Swift. Features Component Libraries Requirements Migration Guides Communication Installation Usage

Alamofire 38.7k Jan 8, 2023
Versatile HTTP Networking in Swift

Net is a versatile HTTP networking library written in Swift. ?? Features URL / JSON / Property List Parameter Encoding Upload File / Data / Stream / M

Intelygenz 124 Dec 6, 2022
🏇 A Swift HTTP / HTTPS networking library just incidentally execute on machines

Thus, programs must be written for people to read, and only incidentally for machines to execute. Harold Abelson, "Structure and Interpretation of Com

John Lui 845 Oct 30, 2022
ServiceData is an HTTP networking library written in Swift which can download different types of data.

ServiceData Package Description : ServiceData is an HTTP networking library written in Swift which can download different types of data. Features List

Mubarak Alseif 0 Nov 11, 2021
Easy HTTP Networking in Swift a NSURLSession wrapper with image caching support

Networking was born out of the necessity of having a simple networking library that doesn't have crazy programming abstractions or uses the latest rea

Nes 1.3k Dec 17, 2022
Extensible HTTP Networking for iOS

Bridge Simple Typed JSON HTTP Networking in Swift 4.0 GET GET<Dict>("http://httpbin.org/ip").execute(success: { (response) in let ip: Dict = respo

null 90 Nov 19, 2022
Http - Demo for Http Layer

http Example To run the example project, clone the repo, and run pod install fro

null 0 Jan 24, 2022
A modern download manager based on NSURLSession to deal with asynchronous downloading, management and persistence of multiple files.

TWRDownloadManager TWRDownloadManager A modern download manager for iOS (Objective C) based on NSURLSession to deal with asynchronous downloading, man

Michelangelo Chasseur 407 Nov 19, 2022