1
我读了关于Alamofire POST请求我设法implemenet它为我的使用文档后,但POST请求保持返回我的代码400,这意味着该请求是坏的。Alamofire POST请求不改变头
我的实现看起来LIK:
let parameters = ["username": name, "password": hashPass]
let url = URL(string: LOGIN_URL)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])
} catch {
}
request.setValue("application/json;charest=utf-8", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON { response in
print("Get default graph request \(response.request!)")
print("Get default graph response \(response.response!)")
if let message = response.result.value {
SessionMenager.Instance.token = message as! String
completion(true)
} else {
completion(false)
}
}
每当我调试代码,我可以看到"Content-Type" = "text/html"
。
我怎么能解决这个问题? 在此先感谢!
编辑
这里是URLSession
一个版本,它的工作原理:
let stringOp = StringOperations.init()
let md5Data = stringOp.MD5(string:pass)
let hashPass = md5Data!.map { String(format: "%02hhx", $0) }.joined()
let json: [String: Any] = ["username": name,
"passwordHash": hashPass ]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
let url = URL(string: LOGIN_URL)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
// insert json data to the request
request.httpBody = jsonData
request.setValue("application/json;charest=utf-8", forHTTPHeaderField: "Content-Type")
print ("REQUEST : \(request)")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
if let httpResponse = response as? HTTPURLResponse {
print("POST : code - \(httpResponse.statusCode)")
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
let message:String = responseJSON["message"] as! String
if !(message.range(of: "ERROR") != nil){
SessionMenager.Instance.token = message
completion(true)
}
} else{
print(error.debugDescription)
}
}
task.resume()
第二个编辑
Al'right!我已经弄清楚如何在好办法做到这一点在我的情况:
let stringOp = StringOperations.init()
let md5Data = stringOp.MD5(string:pass)
let hashPass = md5Data!.map { String(format: "%02hhx", $0) }.joined()
let json: [String: Any] = ["username": name, "passwordHash": hashPass ]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
let url = URL(string: LOGIN_URL)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = jsonData
request.setValue("application/json;charest=utf-8", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON { response in
print("Get default graph request \(response.request!)")
print("Get default graph response \(response.response!)")
let json = JSON(response.data!)
let message = json["message"]
if !(message.stringValue.range(of: "ERROR") != nil) {
SessionMenager.Instance.token = message.stringValue
completion(true)
} else {
completion(false)
}
}
尝试像这样http://stackoverflow.com/questions/42486243/set-content-type-when-performing-get-using- alamofire-42486288分之4#42486288 –
@NiravD其仍与您的实现相同; /我已经编辑与URLSession答案 – yerpy