0

I have a route ("/states"):

func initializeCodableRoutes(app: App) {
    app.router.get("/states", handler: app.getStatesHandler)
}

here is the corresponding handler (Codable handler):

extension App {
    func getStatesHandler(completion: ([State], RequestError?) -> Void) {
        let result = [State(id: 1, name: "تهران"),
                      State(id: 2, name: "آذربایجان شرقی"),
                      State(id: 3, name: "کرمان"),
                      State(id: 4, name: "آذربایجان غربی"),
                      State(id: 5, name: "اردبیل"),
                      State(id: 6, name: "اصفهان"),
                      State(id: 7, name: "البرز"),
                      State(id: 8, name: "ایلام"),
                      State(id: 9, name: "بوشهر"),
                      State(id: 10, name: "چهارمحال و بختیاری"),
                      State(id: 11, name: "خراسان جنوبی"),
                      State(id: 12, name: "خراسان رضوی")]
        completion(result, nil)
    }
}

and this is the code of State struct for more clarity:

struct State: Codable {
    let id: Int
    let name: String
}

when I run my server and try the {baseurl}/states in the browser, I get response like this:

[{"id":1,"name":"تهران"},{"id":2,"name":"آذربایجان شرقی"},{"id":3,"name":"کرمان"},{"id":4,"name":"آذربایجان غربی"},{"id":5,"name":"اردبیل"},{"id":6,"name":"اصÙهان"},{"id":7,"name":"البرز"},{"id":8,"name":"ایلام"},{"id":9,"name":"بوشهر"},{"id":10,"name":"چهارمحال Ùˆ بختیاری"},{"id":11,"name":"خراسان جنوبی"},{"id":12,"name":"خراسان رضوی"},{"id":13,"name":"Test"},{"id":14,"name":"تست"}]

how can I get the correct unicode response?

  • Honestly, I'm not sure, but this may shed some light on your issue. https://forums.swift.org/t/why-is-character-not-codable/56178/15 – Jake May 29 '23 at 01:45
  • how did you turn the response to a json`String` and printed it? Note, `State` is already a Swift word, it is best to use another name for your own struct, it may/will confuse you and the compiler eventually. – workingdog support Ukraine May 29 '23 at 02:54
  • Looks like you are decoding the data you get into `ascii`. Use `utf8` instead, to get the original (json) data string representation. – workingdog support Ukraine May 29 '23 at 06:44

1 Answers1

0

I tried several browsers and saw that for example, firefox shows the correct unicode response(while safari not), so I understood that the problem sits in the decoding side of the client. server-side code works good enough.