Minimalistic Swift HTTP request agent for iOS and OS X

Related tags

Networking agent
Overview

Agent

Table of Contents

Minimalistic Swift HTTP request agent for iOS and OS X.

Introduction

This is a tiny framework that gives you nice a API for crafting HTTP requests.

While Agent is mainly for JSON it also lets you send and access the raw data of a request and response. However if you are looking for a more feature complete library please take a look at Alamofire.

Usage

Throughout this documentation req is used as an instance of Agent.

HTTP Verbs

The Agent API is simple and easy to use. Simply use Agent.<verb>(url) and you're good to go.

Overloading

It's possible to perform an entire request with a single call. Supply the required parameters when first creating the request. There are usually multiple degrees of overloading.

let done = { (response: NSHTTPURLResponse?, data: AnyObject?, error: NSError?) -> Void in
  // react to the result of your request
};
Agent.post("http://example.com", headers: [ "Header": "Value" ],
  data: [ "Key": "Value" ], done: done)

It's possible to omit most overloaded parameters such as headers.

Method Chaining

Every Agent method returns the Agent itself, therefore it is possible to write more expressive code.

Agent.post("http://example.com")
  .send([ "Key": "Value" ])
  .end({ (response: NSHTTPURLResponse?, data: AnyObject?, error: NSError?) -> Void in
    // react to the result of your request
  }
)

Response Closure

One of the features that makes Agent is the response closure, instead of setting up a delegate for every HTTP request you have to make. You can simply react to the response in a closure.

In Agent, the response is of the type (response: NSHTTPURLResponse!, data: Agent.Data!, error: NSError!). A response closure that reads JSON is easily created as seen below.

let done = { (response: NSHTTPURLResponse!, data: Agent.Data!, error: NSError!) -> Void in
  let json = data! as Dictionary<String, String>
  println(json["Key"]!)
}

Verbs

GET(url: String)

let req = Agent.get("http://example.com")
req.end({ (response: NSHTTPURLResponse?, data: AnyObject?, error: NSError?) -> Void in
  // react to the result of your request
})

POST(url: String)

let req = Agent.post("http://example.com")
req.send([ "Key": "Value" ])
req.end({ (response: NSHTTPURLResponse?, data: AnyObject?, error: NSError?) -> Void in
  // react to the result of your request
})

PUT(url: String)

let req = Agent.put("http://example.com")
req.send([ "Key": "Value" ])
req.end({ (response: NSHTTPURLResponse?, data: AnyObject?, error: NSError?) -> Void in
  // react to the result of your request
})

DELETE(url: String)

let req = Agent.delete("http://example.com")
req.end({ (response: NSHTTPURLResponse?, data: AnyObject?, error: NSError?) -> Void in
  // react to the result of your request
})

Methods

send(data: AnyObject) -> Agent

Will JSON serialize any data and send it along as the HTTP body. Also implicitly sets the Content-Type header to application/json.

set(header: String, value: String) -> Agent

Sets the HTTP header to value.

end(done: Response) -> Agent

Will start the request and call done when it's complete.

  • If the request was successful then $0 will be an NSHTTPURLResponse.
  • If the response had any data, $1 will be an AnyObject that you can type cast to either an Array or Dictionary.
  • If there was an error then $2 will be an NSErrror that you can inspect for more information.

NSMutableURLRequest

You can always access the underlying NSMutableURLRequest using req.request.

Contributing

We're happy to receive any pull requests. Right now we're working hard on a number of features as seen below.

  • Complete asynchronous tests
  • Plugins
  • Specialized agents (to handle default headers and such)

Any issue is appreciated.

License

MIT

You might also like...
Type-safe networking abstraction layer that associates request type with response type.

APIKit APIKit is a type-safe networking abstraction layer that associates request type with response type. // SearchRepositoriesRequest conforms to Re

Automatically sets the network activity indicator for any performed request.

BigBrother BIG BROTHER IS WATCHING YOU. BigBrother is a Swift library made for iOS that automatically watches for any performed request and sets the n

YTKNetwork is a high level request util based on AFNetworking.

YTKNetwork What YTKNetwork is a high level request util based on AFNetworking. It's developed by the iOS Team of YuanTiKu. It provides a High Level AP

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.

A frida tool that capture GET/POST HTTP requests of iOS Swift library 'Alamofire' and disable SSL Pinning.
A frida tool that capture GET/POST HTTP requests of iOS Swift library 'Alamofire' and disable SSL Pinning.

FridaHookSwiftAlamofire A frida tool that capture GET/POST HTTP requests of iOS Swift library 'Alamofire' and disable SSL Pinning. 中文文档及过程 Features Ca

A little and powerful iOS framework for intercepting HTTP/HTTPS Traffic.
A little and powerful iOS framework for intercepting HTTP/HTTPS Traffic.

A little and powerful iOS framework for intercepting HTTP/HTTPS Traffic.

Swift/Obj-C HTTP framework with a focus on REST and JSON

Now Archived and Forked PMHTTP will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork of PHMT

A Swift web framework and HTTP server.
A Swift web framework and HTTP server.

A Swift Web Framework and HTTP Server Summary Kitura is a web framework and web server that is created for web services written in Swift. For more inf

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

Releases(0.1.0)
Owner
null
Deal with query items, HTTP headers, request body and more in an easy, declarative way

Reusable system for complex URL requests with Swift. Deal with query items, HTTP headers, request body and more in an easy, declarative way. Check out our engineering blog to learn more!

Parable Health 19 Sep 5, 2022
Http Request wrapper written in Swift

Net Net is a HttpRequest wrapper written in Swift Features GET, POST, PUT, DELETE method Powerful request params: nested params, number, string, dic,

Le Van Nghia 304 Jun 29, 2022
An Generic HTTP Request Library For Swift

GRequest An HTTP request library written in Swift. ##Basic Usage Be simple, as it should be: Request("https://api.github.com/repos/lingoer/SwiftyJSON/

Ruoyu Fu 114 Oct 18, 2022
This package is meant to make http request of an easy way inspiren in the architecture of Moya package

NetworkAgent This package is meant to make http request of an easy way inspiren in the architecture of Moya package. This package is 100% free of depe

Angel Rada 19 Sep 8, 2022
Minimalist HTTP request library via async / await

Making HTTP API requests in Swift using Foundation can be verbose and use a mix of types like URL, URLComponents and URLRequest to form a request and then handling all the encoding and decoding steps later

Nicholas Maccharoli 13 Nov 5, 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
NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. ⚙️🚀

SONetworking NSURLSession network abstraction layer, using Codable and Decodable for response and Encodable for request. Project Folder and File Struc

Ahmad AlSofi 4 Jan 28, 2022
An iOS library to route API paths to objects on client side with request, mapping, routing and auth layers

WANetworkRouting Developed and Maintained by ipodishima Founder & CTO at Wasappli Inc. Sponsored by Wisembly A routing library to fetch objects from a

null 10 Nov 20, 2022
DBNetworkStack is a network abstraction for fetching request and mapping them to model objects

DBNetworkStack Main Features ?? Typed network resources ?? Value oriented architecture ?? Exchangeable implementations ?? Extendable API ?? Composable

DB Systel GmbH 33 Jan 10, 2022
Request adapter for URL requests from "MovieLister" demo app (Swift for Good book, a chapter by Ben Scheirman)

RequestAdapter Request adapter for URL requests from "MovieLister" demo app (Swift for Good book, a chapter by Ben Scheirman) The code is taken from:

Mihaela Mihaljevic Jakic 0 Nov 22, 2021