I'm trying to build a restful API
and I'm struggling on how to serialize JSON
data to a HTTP query string
.
There are a number of mandatory and optional arguments that need to be passed in the request, e.g (represented as a JSON object below):
{
"-columns" : [
"name",
"column"
],
"-where" : {
"-or" : {
"customer_id" : 1,
"services" : "schedule"
}
},
"-limit" : 5,
"return" : "table"
}
I need to support a various number of different clients so I'm looking for a standardized way to convert this json object to a query string. Is there one, and how does it look?
另一种 Select 是允许用户只传递消息体中的json对象,但我读到我应该避免这样做(HTTP GET with request body).
Any thoughts?
Edit for clarification:
Listing how some different languages encodes the given json object above:
- 使用
$.param
:-columns[]=name&;-columns[]=column&;-where[-or][customer_id]=1&;-where[-or][services]=schedule&;-limit=5&;return=column - 使用
http_build_query
:-columns[0]=name&;-columns[1]=column&;-where[-or][customer_id]=1&;-where[-or][services]=schedule&;-limit=5&;return=column -
Perl
usingURI::query_form
: -columns=name&-columns=column&-where=HASH(0x59d6eb8)&-limit=5&return=column -
Perl
usingcomplex_to_query
: -columns:0=name&-columns:1=column&-limit=5&-where.-or.customer_id=1&-where.-or.services=schedule&return=column
jQuery and PHP is very similar. Perl using complex_to_query is also pretty similar to them. But none look exactly the same.