Swiftpack.co - Package - IBM-Swift/SwiftyRequest


Build Status - Master macOS Linux

SwiftyRequest is an HTTP networking library built for Swift.

SwiftyRequest uses URLSession for the underlying transport. URLSession on Linux is not yet completely implemented so you may find that this library is less reliable on Linux than Darwin.



  • Several response methods (e.g. Data, Object, Array, String, etc.) to eliminate boilerplate code in your application.
  • JSON encoding and decoding.
  • Integration with CircuitBreaker library.
  • Authentication token.
  • Multipart form data.

Swift version

The 0.0.x releases were tested on macOS and Linux using the Swift 3.1 and 3.1.1 binaries.

The 1.x.x releases were tested on macOS and Linux using the Swift 4.0.3 and later.

NOTE: Because of issues with URLSession/URLRequest in Swift 4.0, Swift 4.0 projects should use the 0.0.x release of SwiftyRequest.

You can download this version of the Swift binaries by following this link.


To leverage the SwiftyRequest package in your Swift application, you should specify a dependency for it in your Package.swift file:

 import PackageDescription

 let package = Package(
     name: "MySwiftProject",


     dependencies: [
        // Swift 4.0
        .package(url: "https://github.com/IBM-Swift/SwiftyRequest.git", .upToNextMajor(from: "0.0.0")),
        // Swift 4.0.2 or newer
        .package(url: "https://github.com/IBM-Swift/SwiftyRequest.git", .upToNextMajor(from: "1.0.0")),



Make Requests

To make outbound HTTP calls using SwiftyRequest, You create a RestRequest instance. The method parameter is optional (defaulting to .get) and url is required

Below is the list of customizeable fields

  • headerParameters
  • acceptType
  • messageBody
  • productInfo
  • circuitParameters
  • contentType : Defaults to application/json
  • method : Defaults to application/json

Example usage of RestRequest:

import SwiftyRequest

let request = RestRequest(method: .get, url: "http://myApiCall/hello")
request.credentials = .apiKey

Invoke Response

In this example, responseToError is simply an error handling function. The response object we get back is of type RestResponse<String> so we can perform a switch on the response.result to determine if the network call was successful.

request.responseString(responseToError: responseToError) { response in
    switch response.result {
    case .success(let result):
    case .failure(let error):

Invoke Response with Template Parameters

In this example, we invoke a response method with some template parameters to be used in replacing the {state} and {city} values in the url. This allows us to create multiple response invocations with the same RestRequest object, but possibly using different url values. Additionally, the RequestParameters is a helper object to bundle up values used to create a RestRequest object.

let request = RestRequest(url: "http://api.weather.com/api/123456/conditions/q/{state}/{city}.json")
request.credentials = .apiKey

request.responseData(templateParams: ["state": "TX", "city": "Austin"]) { response in
	// Handle response

Invoke Response with Query Parameters

In this example, we invoke a response method with a query parameter to be appended onto the url behind the scenes so that the RestRequest gets executed with the following url: http://api.weather.com/api/123456/conditions/q/CA/San_Francisco.json?hour=9. If no queryItems parameter is set, then all query parameters will be removed from the url if any existed.

let request = RestRequest(url: "http://api.weather.com/api/123456/conditions/q/CA/San_Francisco.json")
request.credentials = .apiKey

request.responseData(queryItems: [URLQueryItem(name: "hour", value: "9")]) { response in
	// Handle response

CircuitBreaker Integration

SwiftyRequest now has additional built-in functionality for leveraging the CircuitBreaker library to increase your application's stability. To make use of this functionality, you just need to provide a CircuitParameters object to the RestRequest initializer. A CircuitParameters object will include a reference to a fallback function that will be invoked when the circuit is failing fast.


Here is an example of a fallback closure:

let fallback = { (error: BreakerError, msg: String) in
    print("Fallback closure invoked... circuit must be open.")


We just initialize the CircuitParameters object and create a RestRequest instance. The only required value you need to set for CircuitParameters is the fallback (everything else has default values).

let circuitParameters = CircuitParameters(timeout: 2000,
                                          maxFailures: 2,
                                          fallback: breakFallback)

let request = RestRequest(method: .get, url: "http://myApiCall/hello")
request.credentials = .apiKey,
request.circuitParameters = circuitParameters

At this point, you can use any of the response methods mentioned in the section below.

Response Methods

There are various response methods you can use based on what result type you want, here they are:

  • responseData returns a Data object.
  • responseObject<T: Codable> returns a Codable object of type T.
  • responseObject<T: JSONDecodable> returns an object of type T.
  • responseArray<T: JSONDecodable> returns an array of type T.
  • responseString returns a String.
  • responseVoid returns Void.


This Swift package is licensed under Apache 2.0. Full license text is available in LICENSE.


Stars: 42
Help us keep the lights on


2.0.3 - Dec 17, 2018

  • Fixed an issue in checking https when URL contains "https" (#46)

2.0.2 - Dec 12, 2018

  • fix: Make ClientCertificate initializer public (#41)
  • resolve Swift 5 compilation warnings (#44)

2.0.1 - Aug 17, 2018

  • Fix building v2.0.0 on iOS

2.0.0 - Aug 8, 2018

  • Update to Swift 4.2
  • Fix queryItems for responseObject<T: Decodable>
  • Support Bearer HTTP authentication
  • Make Result conform to Equatable on Swift 4.1+
  • Support 2-way SSL client certificates

1.0.0 - Nov 15, 2017


  • Migration to Swift 4.0.2.
  • Codable Support

NOTE: Due to a bug in URLSession in Swift 4.0, this release will not work with Swift 4.0. Those users should use 0.x.x release.