代码之家  ›  专栏  ›  技术社区  ›  user9606220

swift ios使用alamofire获取将卷曲转化为swift卷曲

  •  1
  • user9606220  · 技术社区  · 7 年前

    下面是我的卷曲,我想用alamofire作为post请求在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 回复  |  直到 7 年前
        1
  •  0
  •   user9470831 user9470831    7 年前

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

    为自定义标题创建字典:

    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调用。