crysel's avatar

JSON: Malformed UTF-8 characters incorrectly encoded

Hello,

everytime i try to create a User with characters like "ä,ü,ö ... " i get this failure.

{
    "message": "Error encoding model [App\\User] with ID [33] to JSON: Malformed UTF-8 characters, possibly incorrectly encoded",
    "status_code": 500
}

This is my code:

    public function register(Request $request)
    {
        $validator = $this->validateRequest($request);
        if ($validator->fails()) {
            return response()->json($validator->errors(), 404);
        }
        $user = $this->userRepository->insertUser($request);
        $response = $this->response->item($user, new UserTransformer());
        return response()->json([
            'data' => [
                'status' => 201,
                'message' => 'User successfully created.',
                'message_de' => 'Der Benutzer wurde erfolgreich erstellt.'
            ]
        ], 201);
    }

The database is working fine.

Maybe someone of you can help my with this problem.

Thanks.

0 likes
4 replies
ejdelmonico's avatar

Are you using a utf-8 column or a utf8mb4 column? That might your issue depending on your DB. It's a matter of whether it expects 3bytes or 4bytes per character.

1 like
ejdelmonico's avatar

The only other thing I can think of is when the data is encoded, the symbols have to be unescaped by add this JSON_UNESCAPED_UNICODE as the second param to json_encode()

1 like
crysel's avatar
crysel
OP
Best Answer
Level 1

@ejdelmonico thank you for your help. I found out that i had to use mb_strtolower() instead of strtolower() in my code. It's working now!

Please or to participate in this conversation.