Laravel 5 - problem with UTF-8 in a json response

Published 3 years ago by oriceon

Hello, I`m playing with some utf-8 strings in a ajaxcontroller and i get an error: The Response content must be a string or object implementing __toString(), "boolean" given

$address = ['şâţ' => 1];

$response = response()->json($address);
$response->header('Content-Type', 'application/json');
$response->header('charset', 'utf-8');

return $response;

Anyone know a fix for that? Thanks!

3 years ago (60,970 XP)

Did you try to return the plain array? Laravel should transform it into JSON automagically.

$address = ['şâţ' => 1];

return $address;

I tryed and get same error.

UnexpectedValueException in Response.php line 403: The Response content must be a string or object implementing __toString(), "boolean" given.

 in Response.php line 403
at Response->setContent(false) in Response.php line 47
at Response->setContent(array('���' => '1')) in Response.php line 202
at Response->__construct(array('���' => '1')) in Router.php line 1188
at Router->prepareResponse(object(Request), array('���' => '1')) in Router.php line 692
3 years ago (41,700 XP)

@oriceon When you return response json in Laravel as far as i remember it sets headers for you. So you don`t need to repeat that.

Did this code works when you just put 'foo' in your keyname instead of that jibrish?


@Kemito Yes it is workin if i had

$address = ['foo' => 1];

As i see it is a utf-8 problem. I tryed code with and without header. I know that laravel sets by default headers..

3 years ago (41,700 XP)

@oriceon Are you using latest version of laravel? (currently 5.0.21)

Here is screenshot of fresh instalation on Laravel.

Optionaly from 4.1 version (i guess nothing changes for that in 5.0.*) you can pass 4th param to escape utf-8 characters.

return response()->json(['şâţ' => 1], 200, [], JSON_UNESCAPED_UNICODE);

More json_encode (on what is based json response) codes please see


@Kemito i`m using 5.0.20 (21 has some bugs). I guess code is workin cause is supposed to. Is not a complicated stuff :)

I tested now on a fresh installation and is workin. But dono why in my laravel project is not doing well.


May be related to this or not, but a workaround for me was to change the configuration of the FreeTDS driver that I'm using to access the sql server from linux., in /etc/freetds.conf change the version and set the UTF-8 encoding: [global] # TDS protocol version tds version = 4.1 ,to:
[global] # TDS protocol version tds version = 7.2 client charset = UTF-8


1.- create the following function:

function utf8_encode_deep(&$input) { if (is_string($input)) { $input = utf8_encode($input); } else if (is_array($input)) { foreach ($input as &$value) { self::utf8_encode_deep($value); }

    } else if (is_object($input)) {
        $vars = array_keys(get_object_vars($input));

        foreach ($vars as $var) {

2.- utf8_encode_deep($address);

3.- return response()->json($address);

  2. Your http header's incorrect.


        $address = ['şâţ' => 1];
        $responsecode = 200;
        $header = array (
                'Content-Type' => 'application/json; charset=UTF-8',
                'charset' => 'utf-8'
        return response()->json($address , $responsecode, $header, JSON_UNESCAPED_UNICODE);

use dd() instead of return


this also may happen if you have geometry fields


You can edit below function in "Illuminate\Http\JsonResponse.php"

public function __construct($data = null, $status = 200, $headers = [], $options = 0)
        $headers = ['Content-Type' => 'application/json; charset=UTF-8',
            'charset' => 'utf-8'];
        $options = JSON_UNESCAPED_UNICODE;
        $this->encodingOptions = $options;
        parent::__construct($data, $status, $headers);

Please sign in or create an account to participate in this conversation.