R
R
Ruslan Absalyamov2018-10-25 13:45:43
Laravel
Ruslan Absalyamov, 2018-10-25 13:45:43

How to get rid of Malformed UTF-8 characters, possibly incorrectly encoded error?

I tried to change the output of the exception in the Handler, then I get the response Malformed UTF-8 characters, possibly incorrectly encoded.

public function render($request, Exception $exception)
    {
        if ($exception instanceof ValidationException){
            $message = '';
            foreach ($exception->errors() as $error){
                foreach ($error as $rule){
                    $message .= '.'.$rule;
                }
            }

            $response['message'] = substr($message, 2);
            return $this->apiResponse($response);
        }
        return parent::render($request, $exception);
    }

    function apiResponse($response = [], $success = true, $status = 400, $message = '')
    {
        $headers = ['Content-type' => 'application/json; charset=utf-8'];
        $response = [
            'error' => $status,
            'success' => $success,
            'message' => $message,
            'response' => $response ? $response : new \stdClass(),
        ];

        return response()->json($response, $status, $headers, JSON_UNESCAPED_UNICODE);
    }

What's wrong, like I set the encoding in the response?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
funkyloverone, 2020-04-03
@funkyloverone

I think this might help:
https://stackoverflow.com/questions/46305169/php-j...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question