I read The Programming Language Swift by Apple in iBooks, but cannot figure out how to make an HTTP request (something like cURL) in Swift. Do I need to import Obj-C classes
To make HTTP requests is core functionality for modern languages and one of the first things many developers learn when acclimating to new environments. When it comes to Swift there are a fair amount of solutions to this problem both built into the language and by the community. Let’s take a look at some of the most popular ones.
This is the Alamofire Request Example:-
We are going to use Swift on the command line in all of the following examples. This doesn’t mean you can’t copy and paste any of this to Xcode, but Swift has an array of useful command-line utilities that make testing the code in this tutorial easy.
Follow along by opening your terminal and navigating to the directory where you want this code to live. My directory is named SwiftHTTP which you will see in the examples in the rest of this post.
Enter the following to generate an executable command-line project:
swift package init --type executable
This will also generate a Package.swift file for you. With this, we can add dependencies to use Swift Package Manager for installing third-party libraries.
HTTP Requests with URLRequest
First up, we are going to use a built-in API called URLRequest. If you’re following along using the command line, open up main.swift in the Sources directory that was generated from the previous command, and enter the following code:
import Foundation
// Set the URL the request is being made to.
let request = URLRequest(url: NSURL(string: "https://api.nasa.gov/planetary/apod?
api_key=DEMO_KEY")! as URL)
do {
// Perform the request
var response: AutoreleasingUnsafeMutablePointer? = nil
let data = try NSURLConnection.sendSynchronousRequest(request, returning: response)
// Convert the data to JSON
let jsonSerialized = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]
if let json = jsonSerialized, let url = json["url"], let explanation = json["explanation"] {
print(url)
print(explanation)
}
}
That’s all you need to do to make an HTTP request. Head back to your terminal and run the following command, keeping in mind that you might have to modify this to fit the name of your Swift project:
swift build && .build/debug/SwiftHTTP
HTTP Requests with URLSession
URLSession is another popular way to send HTTP requests that are built into the language. URLSession also doesn’t execute requests off of the main thread, which is pretty nifty.
Modify main.swift to include the following code:
import Foundation
let url = URL(string: "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if let data = data {
do {
// Convert the data to JSON
let jsonSerialized = try JSONSerialization.jsonObject(with: data, options:
[]) as? [String : Any]
if let json = jsonSerialized, let url = json["url"], let explanation =
json["explanation"] {
print(url)
print(explanation)
}
} catch let error as NSError {
print(error.localizedDescription)
}
} else if let error = error {
print(error.localizedDescription)
}
}
task.resume()
// Infinitely run the main loop to wait for our request.
// Only necessary if you are testing in the command line.
RunLoop.main.run()
The example we used before with URLRequest was synchronous, but this one is asynchronous. For this reason, we are calling RunLoop.main.run() at the end to make sure the script doesn’t finish executing before the request is responded to. Feel free to take that last line out if you are using this code in a different context.
Just like before, run this code:
swift build && .build/debug/SwiftHTTP
Once you get a response, kill the script with ctrl-c.
HTTP Requests with Alamofire
The previous methods have been built into the language. But there are also third-party networking libraries in Swift. Alamofire is an excellent (and also the most popular) in the Swift community.
Alamofire is a user-friendly and versatile library with a lot of options. It has chainable request and response methods and takes care of boilerplate functionality such as validating HTTP responses.
Unlike the other examples, we need to modify Package.swift for this to work. We’ll also be using another library called SwiftyJSON, which makes parsing JSON more user-friendly and works very well with Alamofire.
Replace the code inside Package.swift with the following to add Alamofire and SwiftyJSON as dependencies:
import PackageDescription
let package = Package(
name: "SwiftHTTP",
dependencies: [
.Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4),
.Package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", majorVersion: 3,
minor: 1)
]
)
Now head back to main.swift to change the code to use Alamofire:
import Foundation
import Alamofire
import SwiftyJSON
Alamofire.request("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY").responseJSON
{ response in
if let result = response.result.value {
let json = JSON(result)
print(json["url"])
print(json["explanation"])
}
}
RunLoop.main.run()
Just like in our previous option, the request is asynchronous so we are calling RunLoop.main.run().