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