REST, PHP - Character Handling of Client POST Data Characters

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

How do I handle the request to prevent character encoding issues?

Should I convert the data sent by the client to UTF-8 before processing it, or should I just accept it utf-8?

I am asking this question as JSON can be encoded in many different ways.

Thank.

+3


source to share


1 answer


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



It would be nice to at least make it explicit in your API documentation that UTF-8 is assumed for the content application/json

. And if the client wants to pass the JSON, coded differently, ask them to transfer to another header Content-Type

, which specifies a character set different from the default, with a header similar to this: Content-Type: application/json; charset=UTF-16

.

+2


source







All Articles