I’m having trouble figuring out the best way to encode the POST parameters to a server call. I writing a C# client that will be served by a PHP server. I want to allow a great deal of flexibility in the parameters, so my current plan is to have a single parameter that I use JSON to encode. For example:
params = {"object":"Main","function":"doecho","params":["echothis...."]}
I’m using the C# WebRequest object and contentType of “application/x-www-form-urlencoded; charset=UTF-8”. The data gets to the server and everything works as expected until I add illegal JSON characters in the data.
For example, if I use the following data, then I can’t do a json_decode on it on the server side. The server appears to automatically turn the %40 into a double-quote (“) when I read it with $this->getRequest()->getParams(); (Zend_Framework).
params = {"object":"Main","function":"doecho","params":["echothis%25%5d%22%40%3d%26...."]}
What is the best practice here? Do I need to base64 encode the data? Is there something obvious I’m missing with the content type or a php setting?
I have full control over the client and server, so I’d like to know what is the right/best thing to do.
While potentially any content-type can be used to upload to HTTP, there are three used in practice:
Due to 2 and 3 being so commonly used (as they are supported by all browser’s for submitting forms), pretty much all server-side tech has things to handle them. So unless the PHP part does something strange, you should be able to use either.
application/x-www-form-urlencoded isn’t appropriate for some data, but is the simplest for what it is used for. It’s pretty much the same as the way query-strings are created for GET form requests, but as POST content.
Hence you want your content to be:
As such the first becomes:
And the second:
Both of which PHP’s built-ins will turn back into the forms in your question.