'Invalid \\u escape sequence' error when trying to create a resource
Modified on: Sat, 18 Nov 2023 5:32 AM2020-01-22
Symptoms
I'm seeing the following error when trying to edit or create a resource:
{
"error": "std::runtime_error",
"message": "Invalid \\u escape sequence"
}
Cause
There must be a more than two byte character in a string endpoint returns to you, examples:
- some Japanese/Chinese characters (mostly 3 and 4 bytes)
- some currency signs: € sign (0xE2, 0x82, 0xAC)
For example you during creation of a customer you may send balance
property:
"balance": "40.399,64 €"
Creation fails with "Invalid \\u escape sequence"
error, the endpoint returned 200 OK response to the contoller along with payload mirroring the one that was sent however the balance
property is different:
"balance": "40.399,64 \u20ac"
The cause is that in PHP prior to 5.4 json_encode() automatically escaped unicode, in PHP 5.4 JSON_UNESCAPED_UNICODE
was introduced:
http://php.net/manual/en/function.json-encode.php
Resolution
Upgrade PHP
on endpoint to 5.4 and aps-php-runtime
>= 2.0-340