邮递员响应200,但我的应用程序没有[ERROR 403] Swift

问题描述 投票:0回答:1

我正在尝试使用本地IP使用服务器,如果我尝试使用邮递员进行身份验证,则会获取数据,但是如果使用我的应用程序,则不会。我不知道怎么回事?

API URL

API URL

响应

APP响应APP RESPONSE

而且我在我的应用程序中使用了以下URL:

http://192.168.0.19:81/api/Auth/login/movil

这是我提出要求的方式

 func login(user: User, completionHandler: @escaping (UserP?)->Void){
        var userData: UserP! = nil
        AF.request(UserRouter.login(user: user)).responseJSON{ response in
            print(user.Username)
            print(user.Password)
            switch(response.result){
            case .success(let response):
                print(response)
                let dict = (response as? [String : Any])!
                if let json = dict["data"] as? [String: Any] {

                    if let jsonData = try? JSONSerialization.data(withJSONObject: json , options: .prettyPrinted)
                    {
                        do {
                            let jsonString = String(data: jsonData, encoding: String.Encoding.utf8)!
                            print(jsonString)
                            userData = try JSONDecoder().decode(UserP.self, from: jsonData)
                            print("Object Converted:")
                        } catch {
                            print("Parsing Failed: ", error.localizedDescription)
                        }
                    }
                }else{
                    print("Credenciales Incorrectas")
                }
                completionHandler(userData)

            case .failure(let error):
                print(error.localizedDescription)
                completionHandler(userData)
            }
        }
    }

ROUTER

 enum UserRouter : APIConfiguration {

     // MARK: -API Configuration
     case login(user: User)
     case create(user: User)
     case update(user: User)
     case getById(id: Int)
     case get
     case delete(id: Int)

     // MARK: -HTTP Methods
     var method: HTTPMethod {
         switch self {
         case .login, .create:
                 return .post
             case .update:
                 return .put
             case .delete:
                 return .delete
             case .get, .getById:
                 return .get
         }
     }

     // MARK: -API Endpoints
     var path: String {
         switch self {
             case .login:
                 return "Auth/login/movil"
             case .create:
                 return "/usuario"
             case .update:
                 return "/usuario"
             case .delete:
                 return "/usuario"
             case .get:
                 return "/usuario"
             case .getById(let id):
                 return "/usuario/\(id)"
         }
     }

     // MARK: -Parameters
     var parameters: RequestParams {
         switch self {
             case .login(let user):
                 return .body(["usuario": user.Name, "password": user.Password])
             case .create(let user):
                 return .body(["user": user])
             case .update(let user):
                 return .body(["user": user])
             case .delete(let id):
                 return .body(["id": id])
             case .get:
                 return .body([:])
             case .getById:
                 return .body([:])
         }
     }

     // MARK: URLRequest
     func asURLRequest() throws -> URLRequest {
         let url = try Constants.Production.apiURL.asURL()

         var urlRequest = URLRequest(url: url.appendingPathComponent(path))

         urlRequest.httpMethod = method.rawValue

         urlRequest.setValue(ContentType.json.rawValue, forHTTPHeaderField: HTTPHeadersField.acceptType.rawValue)
         urlRequest.setValue(ContentType.json.rawValue, forHTTPHeaderField: HTTPHeadersField.contentType.rawValue)
         urlRequest.setValue(Constants.Production.authenticationToken, forHTTPHeaderField: HTTPHeadersField.authentication.rawValue)

         switch parameters {
             case .body(let parameters):
                 urlRequest.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: [])
             case .url(let parameters):
                 let queryParameters = parameters.map { pair in
                     return URLQueryItem(name: pair.key, value: "\(pair.value)")
                 }
                 var components = URLComponents(string: url.appendingPathComponent(path).absoluteString)
                 components?.queryItems = queryParameters
                 urlRequest.url = components?.url
         }

         return urlRequest
     }
 }

非常感谢!

swift api postman alamofire http-status-code-403
1个回答
0
投票

我将user.Name更改为user.Username

错误

 var parameters: RequestParams {
         switch self {
             case .login(let user):
                 return .body(["usuario": user.Name, "password": user.Password])
         }
     }

FIXED

 var parameters: RequestParams {
         switch self {
             case .login(let user):
                 return .body(["usuario": user.Username, "password": user.Password])
         }
     }
© www.soinside.com 2019 - 2024. All rights reserved.