Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mhery's avatar
Level 1

force encoding to work on request made for api by web browser

I am maintaning an API that have some requests made by a front-end app. When I call it from postman, all work well, including its encoding. The response is:

{ "message": "Esse nome de usuário está disponível" }

However, when I make it from browser, I receive:

{"message":"Esse nome de usu\u00e1rio est\u00e1 dispon\u00edvel"}

I already made this [https://medium.com/@DarkGhostHunter/laravel-convert-to-json-all-responses-automatically-c4a72b2fd3ac] tutorial, that creates an middleware forcing the content, and on my project I set it like this, unlike the code it sugests:

$request->headers->set('Accept', 'application/json; charset=UTF-8');
$request->headers->set('Charset', 'utf-8');

In postman, I saw that these headers were not setted when making the request.

Also, I already tried to set in my controller response:

return response()->json([
                'message' => __('auth.nickname_available'), 
            ], 200, ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8']);

I followed this [https://stackoverflow.com/questions/50717005/laravel-encode-json-responses-in-utf-8/50717612] tip. But neither worked.

How can I can make this work well?

0 likes
0 replies

Please or to participate in this conversation.