Swiftpack.co - hathway/JSONRequest as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by hathway.
hathway/JSONRequest v0.10.9
JSONRequest is a tiny Swift library for Synchronous and Asynchronous HTTP JSON requests.
⭐️ 9
🕓 45 weeks ago
.package(url: "https://github.com/hathway/JSONRequest.git", from: "v0.10.9")


JSONRequest is a tiny Swift library to do HTTP JSON requests.

JSONRequest provides a clean and easy-to-use API to submit HTTP requests both asynchronously and synchronously (see Why Synchronous? Are you crazy?).

Synchronous Usage

Synchronous GET

let result = JSONRequest.get("http://httpbin.org/get?hello=world")
if let value = result.data?["args"]??["hello"] as? String {
    print(value) // Outputs "world"

Synchronous POST

let postResult = JSONRequest.post("http://httpbin.org/post", payload: ["hello": "world"])
if let value = postResult.data?["json"]??["hello"] as? String {
    print(value) // Outputs "world"

Asynchronous Usage

Asynchronous GET

JSONRequest.get("http://httpbin.org/get?hello=world") { result in
    if let value = result.data?["args"]??["hello"] as? String {
        print(value) // Outputs "world"

Asynchronous POST

JSONRequest.post("http://httpbin.org/post", payload: ["hello": "world"]) { result in
    if let value = result.data?["json"]??["hello"] as? String {
        print(value) // Outputs "world"

Sending Data to the Server

Query Parameters

URL parameters can be passed as a [String: AnyObject] dictionary and are automatically URL-encoded and appended to the URL string. All requests allow for query parameters, independently of the HTTP method used. Values of the query parameter dictionary will be converted to String before being URL-encoded.

JSON Payload

Response is automatically parsed (valid JSON response is expected) and returned as AnyObject (valid JSON values include Array, Dictionary, Int, Double, String and nil)

Custom Headers

All JSONRequest requests automatically include the following default headers:

Content-Type: application/json
Accept: application/json

The underlining NSMutableURLRequest object can be accessed via the urlRequest property.



JSONRequest uses DVR for testing. DVR records the HTTP interactions of the tests and replays them during future runthroughs.


Each test target should contain a setup function that should look like this:

override func setUp() {
    JSONRequest.requireNetworkAccess = false
    // anything else that needs to be setup

Setting the requireNetworkAccess variable disables errors due to lack of internet, allowing tests to pass wherever you might need to test.

It should be noted that internet access is REQUIRED the first time a test is run so that DVR can record the responses given.

After you have your target setup you can test easily by creating your own instance of JSONRequest and pointing it to the stored response file.

func testMyAmazingChange() {
    let jsonRequest = JSONRequest(session: DVR.Session(cassetteName: "testFiles/testMyAmazingChange"))
    // all network calls and asserts using jsonRequest

The first time the test is run DVR will record the requests and responses made, storing both in the file indicated. When the test has finished DVR will abort testing and print out the location of the saved file which you will need to add to xcode. DVR will only record one cassette per test case, so to re-record a test simply delete the corresponding cassette and re-run the test; DVR will record the interaction and generate a new cassette.

Why Synchronous? Are you crazy?

Usage in iOS Apps

Usage in Command-Line Apps

Usage in Playgrounds


Stars: 9
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

SPM support
45 weeks ago

v0.10.9 SPM support

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics