将 URL 请求从 Android 转换为 IOS

Convert URL Request from Android to IOS

我需要请求获取令牌。

如果我的数据不正确,我会得到“错误数据”的正确答案

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

responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.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()
    }
}

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

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()
            }
        }