ios - 使用服务器用户名和密码在 swift 中发布方法

标签 ios swift web-services alamofire

我正在尝试转换此curl命令(有效):

curl https://address --user appID:appPassword -d grant_type=client_credentials -d scope=read -d format=json

到一个快速的。

我已经有了,但我不知道如何传递我的 appID 和 appPassword:

private func LoadKey() {
    let params : Parameters = ["grant_type":"client_credentials",
                                       "scope":"read",
                                       "format":"json"]

    let url =  "https://address"

    Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.httpBody).response{ response in

          print(response)

    }

}

祝您有美好的一天,提前感谢您的回答。

最佳答案

首先将数据解析为字典,使用.updateValue()函数,然后将它们解析回参数,

var params : [String:String] = ["grant_type":"password",
                                "scope":"read",
                                "format":"json"]

params.updateValue("myPassword", forKey: "grant_type")

如果找不到该 key ,它将使用该值创建一个新 key 。

关于ios - 使用服务器用户名和密码在 swift 中发布方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807461/

相关文章:

ios - 在 FirstViewController 中单击按钮时如何在 SecondViewController 中显示图像

ios - 同步使用 Firebase observeSingleEventOfType

swift - 使用 arrayController 结果为 "Cannot perform operation without a managed object context"

ios - 闭包执行

ios - 从 ios 在 soap web 服务中发送参数

java - REST API 架构

ios - 如何从 XCTestCase(UI 测试用例)调用应用程序代码中的方法

swift - 如何在 UITabbar 中使用所需的投影设置左上角和右上角半径?

ios - 如何在 iOS 中发送和接收 Web 服务请求和响应?

ios - 如何区分本地通知