3

In my RESTful service written in PHP, in order to update a resource, the client sends raw JSON via POST in the request content (not from a form, which means Content-Type: application/json)

How should I handle the request in order to prevent character encoding problems?

Should I convert the data sent by the client to UTF-8 before handling it, or should I just assume it's utf-8?

I'm asking this question since JSON can be encoded in different ways.

Thank you.

Taru
  • 2,562
  • 4
  • 22
  • 30
  • Can you show a bit of code to clarify? I usually url-encode the JSON string, then send it up with the call as a parm. – Jonathan M Aug 15 '14 at 19:33
  • I refer to the case in which the client(which isn't my code) surprises the server with JSON data with different encoding than UTF-8. – Taru Aug 15 '14 at 19:36

1 Answers1

2

I would recommend you write your PHP code to assume all incoming JSON data is encoded as UTF-8, since that's the default in the spec, and certainly the default in most JSON codecs.

It would be a good idea though to make it explicit in your API documentation that UTF-8 is assumed for application/json content. And if a client wants to transmit JSON encoded differently, instruct them to pass a different Content-Type header that specifies the non-default encoding, with a header like this: Content-Type: application/json; charset=UTF-16.

Community
  • 1
  • 1
Brian Kelly
  • 19,067
  • 4
  • 53
  • 55
  • Now after checking was made available to me: If the content sent is UTF-16BE(for example), the PHP string will contain it like a binary, so parsing json will fail anyway. I wanted to take the charset from Content-Type and convert the data to UTF-8, but Symfony2 returns "json" from Request::getContentType(), so the charset wasn't available to me. – Taru Aug 16 '14 at 09:47