小编Dan*_*nny的帖子

在Swift中将cUrl命令转换为HTTP请求

我正在构建一个检查域可用性的应用程序。现在,我正在使用goDaddy API;根据goDaddy的说法,检查URL可用性的方法是通过以下cURL命令:

curl -X GET -H "Authorization: sso-key {API_KEY}:{API_SECRET}" "https://api.godaddy.com/v1/domains/available?domain=example.guru"
Run Code Online (Sandbox Code Playgroud)

现在,我正在尝试将该cURL命令转换为Swift。我正在使用Alamofire;但是,当我发出请求时,出现如下错误:

必须指定凭证

我想知道如何解决这个问题。这是我当前的代码:

class ViewController: UIViewController {

    let key = "KEYKEYKEY"
    let secret = "SECRETSECRET"

    var headers: HTTPHeaders = [:]

    override func viewDidLoad() {
        super.viewDidLoad()

        let credential = URLCredential(user: key, password: secret, persistence: .forSession)

        Alamofire.request("https://api.godaddy.com/v1/domains/available?domain=example.guru")
            .authenticate(usingCredential: credential)
            .responseJSON { response in
                debugPrint(response)
        }

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be …
Run Code Online (Sandbox Code Playgroud)

curl ios swift alamofire godaddy-api

5
推荐指数
1
解决办法
5591
查看次数

标签 统计

alamofire ×1

curl ×1

godaddy-api ×1

ios ×1

swift ×1