【发布时间】:2018-07-18 12:27:12
【问题描述】:
func createPost(username: String, user_id: String, description: String, image: UIImage, completion: @escaping (Bool) -> ()) {
var request = URLRequest(url: URL(string: Globals.root+"/amazing/image/post/here")!)
request.httpMethod = "POST"
//request.httpBody = .data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
if let data = data {
do {
let aaData = try JSONSerialization.jsonObject(with: data, options: [])
if let ppData = aaData as? [String:Any] {
let u = ppData["username"] as! Bool
if u == true {
completion(true)
}
}
} catch let error as NSError {
print(error.localizedDescription)
completion(false)
}
}
}
task.resume()
}
我一直在寻找一个有效的答案,但我没有找到任何与 Swift 4 相关的答案。我希望能够将图像发送到我的 RESTful API 而不对其进行编码。怎么可能做到这一点?提前致谢。 :)
【问题讨论】:
-
使用多部分形式发送图像和其他参数。对于多部分使用,可以使用 Alamofire。
标签: ios swift https swift4 flask-restful