带标题的Alamofire POST请求

use*_*400 4 post http-headers swift alamofire

我试图在Swift中使用Alamofire发出带有标题的帖子请求.但是,我一直在方法调用错误中获取额外参数.我正在使用Alamofire的4.5版本.我无法弄清楚错误.

请找到附带的代码

 let headers = ["Authorization": token, "Content-Type": "application/json"]

 Alamofire.request("http://localhost:8000/create", method: .post,  parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
}
Run Code Online (Sandbox Code Playgroud)

Uma*_*avi 7

以这种方式添加标题

    let headers = ["Authorization" : "Bearer "+accessToken!+"",
                   "Content-Type": "application/json"]



    Alamofire.request(URL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON
            { (response:DataResponse) in
                switch(response.result)
                {
                case .success(let value):
//for Json serialization add in success:

    let JSON = try JSONSerialization.jsonObject(with: response.data! as Data, options:JSONSerialization.ReadingOptions(rawValue: 0))

                            guard let JSONDictionary: NSDictionary = JSON as? NSDictionary else {

                                return
                            }
                        completionHandler(JSONDictionary as? NSDictionary, nil)
                    case .failure(let error):
                        completionHandler(nil, error as NSError?)
                        break
                    }

            }
Run Code Online (Sandbox Code Playgroud)

//为Json序列化添加成功:

    let headers = ["Authorization" : "Bearer "+accessToken!+"",
                   "Content-Type": "application/json"]



    Alamofire.request(URL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON
            { (response:DataResponse) in
                switch(response.result)
                {
                case .success(let value):
//for Json serialization add in success:

    let JSON = try JSONSerialization.jsonObject(with: response.data! as Data, options:JSONSerialization.ReadingOptions(rawValue: 0))

                            guard let JSONDictionary: NSDictionary = JSON as? NSDictionary else {

                                return
                            }
                        completionHandler(JSONDictionary as? NSDictionary, nil)
                    case .failure(let error):
                        completionHandler(nil, error as NSError?)
                        break
                    }

            }
Run Code Online (Sandbox Code Playgroud)