Unable to get Alamofire working

Hello, I am trying to get a basic sample of Alamofire working, but I am unable to do so.

This is for a MacOS command-line app project

I have added the dependency, and code completion works.

But as soon as I run the sample code, no output was returned.

I have reinstalled Xcode several times and the same issue remains.

import Foundation
import Alamofire

print("Hello, World!")
AF.request("https://httpbin.org/get").response { response in
    debugPrint(response)
}

This is the sample code used and the output is

Hello, World!
Program ended with exit code: 0

I can never get the output no matter how I tried
M1 Mac 2021 Xcode 13.2.1 Apple Swift 5.5.2 Alamofire 5.5.0
How can a library be this broken that a simple code cannot be executed?

The library isn't broken, macOS command line tools just don't work the way you think they do. Just putting code in main.swift will simply execute it in a straight line and not wait for asynchronous work to complete before the process exits. You can trigger a wait through the use of dispatchMain() at the end of your code and an exit(0) in the request completion handler, but I like the modern async setup better if you can use Xcode 13.2.1. You'll need to rename your main.swift file to whatever you want to call your executable and add the following:

@main
enum Executable {
    static func main() async {
        print(await AF.request(...).serializingString().response)
    }
}

That should give you a nice async context to experiment with Alamofire's async / await APIs.

4 Likes

Omg, I have no idea that main.swift behaves that way. Thank you for pointing that out.

Much appreciated.