将URL请求从Android转换为IOS



我需要发出一个获取令牌的请求。

如果我的数据不正确,我会得到正确的答案:;错误的数据";

但如果我的数据是正确的,我会得到一个答案:

responseSerializationFailed(原因:Alamofire.AFError.ResponseSerializionFailureReason.inputDataNilOrZeroLength(.

但它适用于Android版本。

Android的请求代码:

@FormUrlEncoded
@POST("api/v1/authorization/login")
fun login(
@Field("username") username: String,
@Field("password") password: String,
@Field("code") code: String
): Single<Response<Void>>

API:

{
"name": "Get token",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "http://192.168.9.41/api/v1/authorization/login?username=%2B79992402979&password=Qwerty%244&code=4242",
"protocol": "http",
"host": [
"192",
"168",
"9",
"41"
],
"path": [
"api",
"v1",
"authorization",
"login"
],
"query": [
{
"key": "username",
"value": "%2B79992402979"
},
{
"key": "password",
"value": "Qwerty%244"
},
{
"key": "code",
"value": "4242"
}
]
}
},
"response": []
}

我的Swift代码:

func confirmLoginWithCode(username: String, password: String, code: String, completion: @escaping () -> Void) {
let parameters: Parameters = [
"username": username,
"password": password,
"code": code
]
let url = "(self.baseUrl)/api/v1/authorization/login"
let authRequest = AF.request(url,
method: .post,
parameters: parameters,
encoding: URLEncoding(destination: .queryString))
authRequest.responseString { (response) in
switch(response.result) {
case .success(let value):
let responseArr = value.components(separatedBy: "u{0022}")
print(responseArr, "success")
case .failure(let error):
print("(error) check error")
}
completion()
}
}

现在我意识到我的问题对网站来说毫无用处,但以下是我的解决方案:事实证明,我只需要查看标题

func confirmLoginWithCode(username: String, password: String, code: String, completion: @escaping () -> Void) {
let parameters: Parameters = [
"username": username,
"password": password,
"code": code
]
let url = "(self.baseUrl)/api/v1/authorization/login"
let authRequest = AF.request(url,
method: .post,
parameters: parameters,
encoding: URLEncoding(destination: .queryString))
authRequest.responseString { (response) in
switch(response.result) {
case .success(let value):
let responseArr = value.components(separatedBy: "u{0022}")
if responseArr[31] == "Wrong verification code!" {
self.confirmLoginStatus = .incorrectCode
}
case .failure(let error):
if let receivedHeaders = response.response?.headers.value(for: "Authorization") {
let token = receivedHeaders.replacingOccurrences(of: "Bearer ", with: "")
self.userSettings.token = token
self.confirmLoginStatus = .success
} else {
self.confirmLoginStatus = .connectionError
}
self.errorStatus = error.errorDescription ?? "some error"
}
completion()
}
}

相关内容

  • 没有找到相关文章

最新更新