Codable router is returning error response in plain text format

Codable router is returning error response in plain text format. Is it possible to get the error response in JSON format for all and every cases?

Can you give an example of your Codable route, the request you're making and the response you're getting? Thanks.

Example:

struct Login: Codable {

var email: String
var password: String

enum CodingKeys: String, CodingKey {
    case email = "email"
    case password = "password"
}

}

let router = Router()
router.post("/api/login", handler: LoginController.post)

class LoginController {

class func post(login: Login, completion: @escaping (Login?, RequestError?) -> Void) {
    completion(nil, RequestError(.badRequest, body: AppError(message: "Unimplemented")))
}

}

CURL Request (We are not sending the password field)

curl -X POST http://localhost:8080/api/login -H 'Accept: /' -H 'Content-Type: application/json' -d '{"email":"soumen@gmail.com"}'

Response:

Could not decode received data: The required key 'password' not found.

As you can see the response is in text format. I am expecting Json here. Maybe something like following:

{
"error": "Could not decode received data: The required key 'password' not found."
}

Thanks for your reply. I'm afraid as you've seen Kitura does not return the error in JSON format. The Content-Type header you have specifies only applies to your POSTed data - not the response (which will not have a JSON content type).

Can you explain why you would expect JSON? As far as I know the REST and HTTP specifications do not say that JSON should be returned - in your case I would expect a 4xx client error status code to be returned.

I just wanted to keep the client server communication always in JSON format so I do not have to deal with two different data format at client app. Although it is not a big deal, I could always check the Content-Type coming from server and accordingly process the Body data. Anyways, Thanks again.

If you control both the client and server, you can do client-side validation so you don't make an invalid request?

Right!

Terms of Service

Privacy Policy

Cookie Policy