swift ios使用alamofire将转换卷曲转换为快速卷曲

时间:2018-04-10 03:53:09

标签: ios swift curl header alamofire

我有下面的卷曲我希望使用alamofire作为帖子请求在swift中执行它,有没有人有这个想法?我的代码有什么问题。

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)
//                }
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

您应该尝试使用自定义每请求标头。

为自定义标题创建字典:

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

HTTPHeaders只是一个字符串字典:

public typealias HTTPHeaders = [String: String]

然后在标题中将标题作为参数传递以创建请求:

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

要确保发送标头,可以使用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呼叫。