ios af 返回html,ios - Alamofire requests with response type of text/html - Stack Overflow

I am converting an Objective C project into swift and as a result, I need to use Alamofire instead of AFNetworking. The server I need to make HTTP calls to expects parameters in JSON format but responds in text/html. When I test the requests in Objc, it works but on swift and alamofire, the response data is nil. I have done this previously using AFNetworking responseSerializer and set the acceptable content types to all 4 possible types. I just can't seem to make this work with Alamofire. I have used any help and tutorials out there but I am banging my head to the ceiling each time. Te errors I get are usually Unable to determine byte size or responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength.

Here is sample code I have used so far:

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

}

}

then tried setting the acceptable content types:

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 of the methods I have tried work. Can anyone help me please? Thanks a million!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值