x-www-form-urlencoded Vs json HTTP POST [英] x-www-form-urlencoded Vs json HTTP POST
问题描述
很难决定,
目前我以x-www-form-urlencoded与php lib curl与
It's hard to decide,
currently I'm sending data as x-www-form-urlencoded with php lib curl with
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($this->arguments));
或
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->arguments);
第一个问题:第二个似乎内容长度较大,可能更好?
first question: second one seems to be larger content length, first solution is probably better?
这是实用的平面消息,如:
It's practical for flat messages like:
{
"name": "John",
"token": "2121232145",
"code": "7",
"data": "Hello"
}
但我也可以有一个数据字段来表示一个对象,在这种情况下我是enconding它, (url编码一个Json)是非常冗长和丑陋的消息,
But I can have also a data field that represent a object, in this case I was enconding it, but doing that (url encoding a Json) is terribly verbose and ugly messages,
另一方面,我尝试发送它作为application / json content-type
On the other side I tried sending it as application/json content-type
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($this->arguments));
内容长度对于小消息而言更大,但是对于嵌入json,显然更好
the content-length is larger for small messages but with embedded json, it's clearly better
但是x-www-form-urlencoded也接近我需要发送的表单数据,除非嵌入了json。
But x-www-form-urlencoded is also close to the forms data I need to send, except when a json is embedded
根据内容类型,有两种不同的servlet解析方法不是很优雅,所以还有另一个选项?
It would not be elegant to have 2 differents servlet parse methods depending on the content types, so is there another option?
推荐答案
这里您可以阅读关于格式的类似讨论。 / p>
Here you can read similar discussion about formats.
如果编码数据的结构被保证为
名称 - 值对的单位列表,x-www-form- urlencoded似乎足够了。如果
结构可以是(任意)复杂的(例如嵌套列表或
关联数组),那么肯定使用JSON。
If the structure of encoded data is guaranteed to be a flat list of name-value pairs, x-www-form-urlencoded seems sufficient. If the structure could be (arbitrarily) complex (e.g. nesting lists or associative arrays), then definitely use JSON.
对我来说,我是KISS的擅长者。在你的情况下JSON / XML /任何额外的成本在时间,内存和CPU周期。 x-www-form-urlencoded数据结合了可读性和紧凑性,所以我可以打赌它是你的选择。
As for me, I'm the KISS adept. In your situation JSON/XML/whatever is extra costs in time, memory and CPU cycles. x-www-form-urlencoded data combine readability and compactness so i can bet it's your choice.
这篇关于x-www-form-urlencoded Vs json HTTP POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!