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

Published 2 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!

bart
bart
2 years ago (56,780 XP)

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

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

return $address;
oriceon

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
Kemito
Kemito
2 years ago (35,500 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?

oriceon

@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..

Kemito
Kemito
2 years ago (35,500 XP)

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

https://ziedlejas.lv/screenshots/img.2015.03.27.ncvy9r.png

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 http://php.net/manual/en/function.json-encode.php

oriceon

@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.

Jacq

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

jcarmonamx

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); }

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

        foreach ($vars as $var) {
            self::utf8_encode_deep($input->$var);
        }
    }
}

2.- utf8_encode_deep($address);

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

ramazanulucay
  1. You use JSON_UNESCAPED_UNICODE
  2. Your http header's incorrect.

Samples;

        $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);
    ```
lifesound

use dd() instead of return

msharaf

this also may happen if you have geometry fields

hamidreza.samsami

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.