Skip to content

Requests is a lightweight asynchronous HTTP Requests library written in Swift.

License

Notifications You must be signed in to change notification settings

peterentwistle/requests

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Requests

Swift Build Status Licence

Requests is a lightweight asynchronous HTTP Requests library written in Swift.

Currently supported HTTP request methods

Further HTTP request methods will be implemented at a later date.


Examples:

Basic usage

import Requests

Requests.get("http://example.com") { response in
    print(response.text)
}

Requests.get("http://httpbin.org/ip") { response in
    print(response.text)
}

Output

<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
...
{
    "origin": "127.0.0.1"
}

JSON Decoding Example

import Requests

struct IP: Decodable {
    var origin: String
}

Requests.get("http://httpbin.org/ip") { response in
    let json: IP = response.json()

    print(json.origin)
}

Output

127.0.0.1

HTTP methods

GET

Requests.get("http://httpbin.org/get") { response in
    print(response.text)
}

POST

Requests.post("http://httpbin.org/post") { response in
    print(response.text)
}

With data

Requests.post("http://httpbin.org/post", data: ["key": "value"]) { response in
    print(response.text)
}

PUT

Requests.put("http://httpbin.org/put") { response in
    print(response.text)
}

With data

Requests.put("http://httpbin.org/put", data: ["key": "value"]) { response in
    print(response.text)
}

PATCH

Requests.patch("http://httpbin.org/patch") { response in
    print(response.text)
}

With data

Requests.patch("http://httpbin.org/patch", data: ["key": "value"]) { response in
    print(response.text)
}

DELETE

Requests.delete("http://httpbin.org/delete") { response in
    print(response.text)
}

With data

Requests.delete("http://httpbin.org/delete", data: ["key": "value"]) { response in
    print(response.text)
}

Authentication

Bearer Authentication

let bearerAuthentication = BearerAuthentication(token: "your-token")

Requests.get("https://httpbin.org/bearer", authentication: bearerAuthentication) { response in
    print(response.text)
}