Jessica Kimble
Jessica Kimble

Reputation: 535

Is there a way to differentiate between success and error API answers in Swift when there is no request status in response?

I'm creating a login page, and I used to handle API error such as:

{"status":"error","answer":"Password is wrong"}

this way:

if error?["status"] as? String == "error" {
 Alert.showBasic(title: error!["status"] as! String, message: error!["answer"] as! String, vc: self)

This worked perfectly. But now I'm working with another API, that responds to my request this way:

{"password":["password is wrong"]}

The confusion what I have, is that I don't get from API answer, a status for my request, like success or error.

Is there a way to handle it?

Upvotes: 0

Views: 185

Answers (1)

Digs
Digs

Reputation: 193

The error != nil depends and what you feed to your model and how you feed depends on status code

 if let httpResponse = response as? HTTPURLResponse{
            switch httpResponse.statusCode {
               case 200 : //handle Root data model
               case 401 : //handle wrong password model
            }
        }

URLSession error is different . Its when the request is failed and no response . But you got the response but its up to you differentiate whether its good or bad.
Server response should be static if there is a change not noticed to you means something wrong in the backend.

Upvotes: 1

Related Questions