Foren

How to set REST API character encoding

thumbnail
Jan Beran, geändert vor 8 Jahren.

How to set REST API character encoding

Junior Member Beiträge: 44 Beitrittsdatum: 30.06.15 Neueste Beiträge
Hello everybody

I recently ran into a problem with using the portal REST services to upload data to the portal. I programmed a small application that read Excel files and makes them into DDL lists and KB articles by sending them with the REST API.
Everything was working fine while I was using the localhost portal to get the application running. But then I switched to the testing portal environment, and I encountered a small problem with characters encoding.
All the data that I send through the REST API have non-English characters (áíé and similar) replaced with Latin characters. The conversion chart is same as here. So the trouble is with UTF-8 encoding being interpreted as Windows-1252.
I was not yet able to view the database data directly, but when I request them through the REST APi, they are already garbled.
Also, all data entered in the portal GUI are fine even with non-English characters.

My question is, how to check (and also set) the database and application server encoding to UTF-8 to interpret the data sent through REST APi correctly and show them in the right way?

Any advice is much appreciated, thanks.
thumbnail
David H Nebinger, geändert vor 8 Jahren.

RE: How to set REST API character encoding

Liferay Legend Beiträge: 14915 Beitrittsdatum: 02.09.06 Neueste Beiträge
Database is outside of Liferay forums and it will vary. For example, with mysql you have to indicate utf8 characterset when you're creating the database.

Application servers, well I've never had to switch one on/off so I just kind of assume that they will support it automagically. If you're running on a windows server that's probably a bad assumption, but I couldn't tell you how to go about fixing it.
thumbnail
Jan Beran, geändert vor 8 Jahren.

RE: How to set REST API character encoding

Junior Member Beiträge: 44 Beitrittsdatum: 30.06.15 Neueste Beiträge
Thanks for the reply David.

So, I digged deep and found what the problem was. Some of the accented characters are encoded as two bytes in UTF-8. For example, á is encoded as %C3%A1. This was fine while using the Hypersonic database at my localhost. But the MySQL database of the testing portal reads the REST URL one byte at a time and understands the above example as á.
A partial solution for this is to encode all problematic characters as HTML entities, i.e á becomes á. The database consumes such request without problem. The data are then stored with the HTML entities in the database, but are displayed OK in the portal GUI.

Is there any better solution to this, that would store the data in a correct way?

Another interesting problem, that came along while finding this solution - when I changed the default portal language to english, the database still consumed the REST service, but was only creating empty records in the DDL, i.e. no data were saved. Is that a bug or feature?
thumbnail
David H Nebinger, geändert vor 8 Jahren.

RE: How to set REST API character encoding

Liferay Legend Beiträge: 14915 Beitrittsdatum: 02.09.06 Neueste Beiträge
Do you know what character set your mysql database was created using?

USE db_name;
SELECT @@character_set_database;
thumbnail
Jan Beran, geändert vor 8 Jahren.

RE: How to set REST API character encoding

Junior Member Beiträge: 44 Beitrittsdatum: 30.06.15 Neueste Beiträge
Hi David

The database uses UTF-8 character set.