响应类型为 text/html 的 Alamofire 请求
Alamofire requests with response type of text/html
我正在将一个 Objective C 项目转换为 swift,因此,我需要使用 Alamofire 而不是 AFNetworking。我需要进行 HTTP 调用的服务器需要 JSON 格式的参数,但以 text/html 格式响应。当我在 Objc 中测试请求时,它可以工作,但在 swift 和 alamofire 上,响应数据为零。我之前使用 AFNetworking responseSerializer 完成了此操作,并将可接受的内容类型设置为所有 4 种可能的类型。我似乎无法使用 Alamofire 来完成这项工作。我已经使用了那里的任何帮助和教程,但每次我都把头撞到天花板。我得到的错误通常是 Unable to determine byte size 或 responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength.
这是我目前使用的示例代码:
let urlString = "\(K.APIKs.kInsecureProtocol)\(K.APIKs.KBaseURL)\(K.APIKs.kSignupURL)"
let parameters: Parameters = [K.SignUPKs.kMSISDN: number]
AF.request(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default).responseJSON { (response) in
switch response.result {
case .success:
self.authorisingSignUpDelegate?.onAuthorisingSignUpSuccess()
break
case .failure(let error):
self.authorisingSignUpDelegate?.onAuthorisingSignUpFailed(error: error)
break
}
}
然后尝试设置可接受的内容类型:
if let url = URL(string: urlString) {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = HTTPMethod.post.rawValue
urlRequest.addValue("application/json", forHTTPHeaderField: "content-type")
urlRequest.addValue("text/html", forHTTPHeaderField: "accept")
urlRequest.httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
let request = AF.request(urlRequest)
.response { response in
print("Request: \(String(describing: response.request))") // original URL request
print("---------------------------")
print("HTTP URL response: \(String(describing: response.response))") // HTTP URL response
print("---------------------------")
print("Data: \(String(describing: response.data))") // server data
print("---------------------------")
print("Result of Reponse Serialization \(response.result)") // result of response serialization
print("---------------------------")
print("Error \(response.error)") // result of response serialization
print("---------------------------")
print("JSON: \(response.result)")
}
}
None 我试过的方法都有效。谁能帮帮我吗?百万感谢!
对于那些有类似问题的人,我写了如下修复:
if let returnedData = JSONData {
do {
let dict = try JSONSerialization.jsonObject(with: returnedData, options: []) as? [String: String]
DataManager().synchronisePassword(from: dict!)
} catch {
print(error.localizedDescription)
}
}
我正在将一个 Objective C 项目转换为 swift,因此,我需要使用 Alamofire 而不是 AFNetworking。我需要进行 HTTP 调用的服务器需要 JSON 格式的参数,但以 text/html 格式响应。当我在 Objc 中测试请求时,它可以工作,但在 swift 和 alamofire 上,响应数据为零。我之前使用 AFNetworking responseSerializer 完成了此操作,并将可接受的内容类型设置为所有 4 种可能的类型。我似乎无法使用 Alamofire 来完成这项工作。我已经使用了那里的任何帮助和教程,但每次我都把头撞到天花板。我得到的错误通常是 Unable to determine byte size 或 responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength.
这是我目前使用的示例代码:
let urlString = "\(K.APIKs.kInsecureProtocol)\(K.APIKs.KBaseURL)\(K.APIKs.kSignupURL)"
let parameters: Parameters = [K.SignUPKs.kMSISDN: number]
AF.request(urlString, method: .get, parameters: parameters, encoding: JSONEncoding.default).responseJSON { (response) in
switch response.result {
case .success:
self.authorisingSignUpDelegate?.onAuthorisingSignUpSuccess()
break
case .failure(let error):
self.authorisingSignUpDelegate?.onAuthorisingSignUpFailed(error: error)
break
}
}
然后尝试设置可接受的内容类型:
if let url = URL(string: urlString) {
var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = HTTPMethod.post.rawValue
urlRequest.addValue("application/json", forHTTPHeaderField: "content-type")
urlRequest.addValue("text/html", forHTTPHeaderField: "accept")
urlRequest.httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
let request = AF.request(urlRequest)
.response { response in
print("Request: \(String(describing: response.request))") // original URL request
print("---------------------------")
print("HTTP URL response: \(String(describing: response.response))") // HTTP URL response
print("---------------------------")
print("Data: \(String(describing: response.data))") // server data
print("---------------------------")
print("Result of Reponse Serialization \(response.result)") // result of response serialization
print("---------------------------")
print("Error \(response.error)") // result of response serialization
print("---------------------------")
print("JSON: \(response.result)")
}
}
None 我试过的方法都有效。谁能帮帮我吗?百万感谢!
对于那些有类似问题的人,我写了如下修复:
if let returnedData = JSONData {
do {
let dict = try JSONSerialization.jsonObject(with: returnedData, options: []) as? [String: String]
DataManager().synchronisePassword(from: dict!)
} catch {
print(error.localizedDescription)
}
}