gpt4 book ai didi

ios - swift ios 使用 alamofire 将 curl 转换为 swift curl

转载 作者:行者123 更新时间:2023-11-28 07:50:52 24 4
gpt4 key购买 nike

我在下面有我的 curl 我想使用 alamofire 作为发布请求快速执行它,有人有想法吗?我的代码有什么问题。

curl curl -H "Content-Type: application/json"      -H "Authorization: Bearer 05BE1EA85FDC05919A37ADF7E93fF518"      -X POST "https://b28831d6-7859-4e19f-a929-7ec4b73f6acb.pushnotifications.pusher.com/publish_api/v1/instances/b28d831d6-7859-4e19-a929-7ec4b73f6acb/publishes"      -d '{"interests":["hello"],"apns":{"aps":{"alert":{"title":"Hello","body":"Hello, world!"}}}}'
{"publishId":"pubid-fc883ab1-0c7f-420a-b864-7ccb1c04cc78"}
4LOOP-MAC-MINI:Fridgeboard_iOS-master-2 test.tesst$

一个

lamofire code

func sendTextToServer(text: String) {
//
let headers:HTTPHeaders = ["Authorization": "Bearer 05BE1EA85FDC05919A37ADF7E9d3F518", "Content-Type": "application/json"]

//
let parameters: Parameters = "["interests":["hello"],"apns":["aps":["alert":"title":"Hello","body":"Helloworld!"]"


Alamofire.request("https://b28831d6-785d9-4e19-a929-7ec4b73f6acb.pushnotifications.pusher.com/publish_api/v1/instances/b28831d6-7859-4e19-a929-7ec4b73f6dacb/publishes", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in

if let dictionary = response.result.value as? Dictionary<String,AnyObject> {
let json = JSON(dictionary)
// if let textToSpeech = json["result"]["fulfillment"]["speech"].string {
// self.speakText(text: t extToSpeech)
// }
}
}

}

最佳答案

您应该尝试使用 Custom Per Request Header。

为您的自定义标题创建字典:

let headers: HTTPHeaders = ["Accept": "application/json"]

HTTPHeaders 只是一个字符串字典:

public typealias HTTPHeaders = [String: String]

然后将 header 作为参数传递到您的调用中以创建请求:

// In the empty quotes, enter you API url info.
let urlString = "" Alamofire.request(urlString, headers: headers).responseJSON { ... }

要确保您的 header 已发送,您可以使用 debugPrint:

let headers: HTTPHeaders = ["Accept": "application/json"]

//In the empty quotes, enter you API url info.
let request = Alamofire.request("", headers: headers)
.responseJSON { response in
// ...
}
debugPrint(request)

从那里开始,您就可以创建 POST 调用了。

关于ios - swift ios 使用 alamofire 将 curl 转换为 swift curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49745030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com