Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

From Alamofire to BothamNetworking

Davide Mendolia edited this page Jan 20, 2016 · 2 revisions

Usage

Making a Request

import Alamofire

Alamofire.request(.GET, "https://httpbin.org/get")
import BothamNetworking

let httpbinClient = BothamAPIClient(baseEndpoint: "https://httpbin.org")
httpbinClient.GET("/get")

Response Handling

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
         .responseJSON { response in
             print(response.request)  // original URL request
             print(response.response) // URL response
             print(response.data)     // server data
             print(response.result)   // result of response serialization

             if let JSON = response.result.value {
                 print("JSON: \(JSON)")
             }
         }
httpbinClient.GET("/get", parameters: ["foo": "bar"]) { result in
    if case let .Success(response) = result {
        print(response)
        print(response.body)
    }
    result.mapJSON { json -> () in
        print(json)
    }
}

Response Handler

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
         .response { request, response, data, error in
             print(request)
             print(response)
             print(data)
             print(error)
          }
httpbinClient.GET("/get", parameters: ["foo": "bar"]) { result in
    if case let .Success(response) = result {
        print(response)
        print(response.body)
    } else if case let .Failure(error) = result {
        print(error)
    }
}

Response JSON Handler

Alamofire.request(.GET, "https://httpbin.org/get")
         .responseJSON { response in
             debugPrint(response)
         }
httpbinClient.GET("/get") { result in
    result.mapJSON { json -> () in
        debugPrint(json)
    }
}
Alamofire.request(.POST, "https://httpbin.org/post")

Alamofire.request(.PUT, "https://httpbin.org/put")

Alamofire.request(.DELETE, "https://httpbin.org/delete")
httpbinClient.POST("/post")

httpbinClient.PUT("/put")

httpbinClient.DELETE("/delete")

Parameters

GET Request With URL-Encoded Parameters

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
// https://httpbin.org/get?foo=bar
httpbinClient.GET("/get", parameters: ["foo": "bar"])
// https://httpbin.org/get?foo=bar

POST Request With URL-Encoded Parameters

let parameters = [
    "foo": "bar",
    "baz": ["a", 1],
    "qux": [
        "x": 1,
        "y": 2,
        "z": 3
    ]
]

Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters)
// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3
let body = [
    "foo": "bar",
    "baz": ["a", 1],
    "qux": [
        "x": 1,
        "y": 2,
        "z": 3
    ]
]

httpbinClient.POST("/post", body: body)
// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3

POST Request with JSON-encoded Parameters

let parameters = [
    "foo": [1,2,3],
    "bar": [
        "baz": "qux"
    ]
]

Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters, encoding: .JSON)
// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}
let body = [
    "foo": [1,2,3],
    "bar": [
        "baz": "qux"
    ]
]

httpbinClient.POST("/post", body: body, headers: ["Content-Type": "application/json"])
// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}