Adding non-ascii characters through the REST API.

677 views
Skip to first unread message

MattK

unread,
Oct 12, 2015, 1:49:40 AM10/12/15
to Firebase Google Group
Hi,

I'd previously created a simple comment storing system in Firebase using the REST API from C++ and recently noticed it failed when Chinese characters where included in the values.

This is an example of the packets I'm trying to send:

POST / HTTP/1.1
Accept: */*
User-Agent: fireClient/1.0
Connection: keep-alive
Keep-Alive: timeout=0, max=0
Content-Type: application/json; charset=utf-8
Content-Length: 198
Pragma: no-cache
{
"CommentObject":
{
"Comment": "是",
"Location": "X=1.000 Y=2.000 Z=3.000",
"Rotation": "P=5.000000 Y=6.000000 R=4.000000",
"Date": "2015.10.12-15.24.48",
"Resolved": false
}
}

It's all correctly encoded in UTF-8, I've checked it multiple times.

Unfortunately, the first time I run the program I get HttpCode 400 with the error: "Invalid data; couldn't parse JSON object array or value.Perhaps you're using invalid characters in your key names."
Thereafter, each time I run it I get HttpCode 405 with the error: "error: unsupported request type"

Does anyone know what could be causing this?

Thanks,

Matt.

Frank van Puffelen

unread,
Oct 12, 2015, 7:09:44 PM10/12/15
to Firebase Google Group
I just ran this command without problems:

 curl -X POST -d '{ "name": "是" }' https://mine.firebaseio.com/.json


MattK

unread,
Oct 12, 2015, 7:19:31 PM10/12/15
to Firebase Google Group
Damn, well, at least I know the problem's on my end somewhere :p Thanks for testing that for me!

Unfortunately I'm building a plugin that sits on top of another plugin that interfaces with Unreal Engine 4's HttpRequest module that utilises curl, so debugging it is a bit difficult.

MattK

unread,
Oct 13, 2015, 12:55:47 AM10/13/15
to Firebase Google Group
In case anyone's curious, the problem was simply that one of the plugins I was using was setting the Content-Length header to the length of the string, which is fine if all your characters are one byte, as they would be with an ascii-style string, but once you start adding characters with multiple bytes, such as Chinese characters, the Content-Length would no longer match the actual number of bytes. The answer was simply to convert the string into utf-8 bytes and use the length of that array as the Content-Length.

Frank van Puffelen

unread,
Oct 13, 2015, 2:56:57 PM10/13/15
to Firebase Google Group
Thanks for reporting back Matt. That indeed makes sense. I wasn't sure what the Content-Length should be (in characters or bytes), but forgot to look it up.
Reply all
Reply to author
Forward
0 new messages