A Swift wrapper around libcurl that works with Linux.
Because curl_easy_setopt
is a variadic function and so is not imported by Swift,
additional wrapper functions are provided to get around the problem, such as:
curl_easy_setopt_string
curl_easy_setopt_bool
For Linux you need:
sudo apt-get install libcurl4-openssl-dev
This component is set up to be used with the Swift Package Manager.
Add this line to your dependencies
section:
.package(url: "https://github.com/in2core/CCurl.git", .branch("master"))
In a new directory, create Package.swift
:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "CCurlTest",
products: [
.executable(name: "CCurlTest", targets: ["CCurlTest"])
],
dependencies: [
.package(url: "https://github.com/in2core/CCurl.git", .branch("master"))
],
targets: [
.target(name: "CCurlTest", dependencies: ["CCurl"], path: ".")
]
)
Now create main.swift
:
import CCurl
let handle = curl_easy_init()
curl_easy_setopt_string(handle, CURLOPT_URL, "http://www.example.com")
curl_easy_setopt_bool(handle, CURLOPT_VERBOSE, true)
let ret = curl_easy_perform(handle)
if let error = curl_easy_strerror(ret) {
print("error = \(error)")
}
print("ret = \(ret)")
Build the app: swift build
Run the app: .build/debug/CCurlTest