1
Alamofireの投稿要求に関する文書を読んだ後、私はその用法を実装することができますが、投稿要求はコード400を返しています。Alamofireの投稿要求でヘッダーが変更されない
私の実装では、リクになります
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"
を見ることができます。
どうすればこの問題を解決できますか? ありがとうございます!
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()
SECOND EDIT
Al'right:ここ
EDIT
作品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"
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-4/42486288#42486288 –
@NiravDあなたの実装とまだ同じです;/URLSessionで回答を編集しました – yerpy