TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

W3C HTML JSON form submission

262 pointsby ozcanesenover 10 years ago

21 comments

rspeerover 10 years ago
Let me say first of all that I&#x27;m glad they&#x27;re working on standardizing this. When making REST APIs, I find HTML form scaffolds incredibly useful, but it means that you probably have to accept both JSON (because JSON is reasonable) and occasional form-encoding (because forms), leading to subtle incompatibilities. Or you have to disregard HTML and turn your forms into JavaScript things that submit JSON. Either way, the current state is ugly.<p>Here&#x27;s the part that I don&#x27;t particularly like, speaking of subtle incompatibilities:<p><pre><code> EXAMPLE 2: Multiple Values &lt;form enctype=&#x27;application&#x2F;json&#x27;&gt; &lt;input type=&#x27;number&#x27; name=&#x27;bottle-on-wall&#x27; value=&#x27;1&#x27;&gt; &lt;input type=&#x27;number&#x27; name=&#x27;bottle-on-wall&#x27; value=&#x27;2&#x27;&gt; &lt;input type=&#x27;number&#x27; name=&#x27;bottle-on-wall&#x27; value=&#x27;3&#x27;&gt; &lt;&#x2F;form&gt; &#x2F;&#x2F; produces { &quot;bottle-on-wall&quot;: [1, 2, 3] } </code></pre> I&#x27;ve seen this ugly pattern before in things that map XML to JSON. Values spontaneously convert to lists when you have more than one of them. Here come some easily overlooked type errors.<p>I don&#x27;t know of any common patterns for working with &quot;a thing or a list of things&quot; in JSON; that kind of type mixing is the thing you hope to get away from by defining a good API. But all code that handles HTML JSON is going to have to deal with these maybe-list-maybe-not values, in a repetitive and boilerplatey way.<p>I hope that a standard such as this will eventually be adopted by real-life frameworks such as Django REST Framework, but I also hope that they just reject the possibility of multiple fields with the same name.
评论 #8666495 未加载
评论 #8665929 未加载
评论 #8666218 未加载
bkardellover 10 years ago
In fairness, you have to look at how standards get somewhere - this is an editor&#x27;s draft which is a starting point of an idea rather than a done deal. Don&#x27;t be surprised if the final product winds up being significantly different than this - even better, get involved in the conversation to make it what we need. That&#x27;s not to pour cold water on it: It&#x27;s good as it is, but there are changes which potentially help explain the magic of participating in form encoding and submission which may be better and allow more adaptation and experimentation over time.
kijinover 10 years ago
Why such an emphasis on &quot;losing no information&quot; when the form is obviously malformed?<p>You need only to look at the crazy ways in which MySQL mangles data to realize that silently &quot;correcting&quot; invalid input is not the way to go. The web has suffered enough of that bullshit, we seriously don&#x27;t need another. Example 7 (mixing scalar and array types) gives me shudders. Example 10 (mismatched braces) seems to have a reasonable fallback behavior, though I&#x27;d prefer dropping the malformed field altogether.<p>If the form is obviously malformed, transmission <i>should</i> fail, and it should fail as loudly and catastrophically as possible, so that the developer is forced to correct the mistake before the code in question ever leaves the dev box.<p>Preferably, the form shouldn&#x27;t even work if any part of it is malformed. If we&#x27;re too timid to do that, at least we should leave out malformed fields instead of silently shuffling them around. Otherwise we&#x27;ll end up with frameworks that check three different places and return the closest match, leaving the developer blissfully ignorant of his error.<p>While we&#x27;re at it, we also need strict limits on valid paths (e.g. no mismatched braces, no braces inside braces) and nesting depth (most frameworks already enforce some sort of limit there), and what to do when such limits are violated. Again, the default should be a loud warning and obvious failure, not silent mangling to make the data fit.<p>This is supposed to be a new standard, there&#x27;s no backward-compatibility baggage to carry. So let&#x27;s make this as clean and unambiguous as possible!
评论 #8667055 未加载
luikoreover 10 years ago
I don&#x27;t agree with Example 9, we should use data uri scheme for file content<p><pre><code> &quot;files&quot;: [{ &quot;name&quot;: &quot;dahut.txt&quot;, &quot;src&quot;: &quot;data:text&#x2F;plain;base64,REFBQUFBQUFIVVVVVVVVVVVVVCEhIQo=&quot; }] </code></pre> <a href="http://en.wikipedia.org/wiki/Data_URI_scheme" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Data_URI_scheme</a>
评论 #8682551 未加载
hughesover 10 years ago
<p><pre><code> { &quot;name&quot;: &quot;Bender&quot; , &quot;hind&quot;: &quot;Bitable&quot; , &quot;shiny&quot;: true } </code></pre> Who puts commas at the <i>start</i> of a continuing line? What good could that possibly do?
评论 #8665422 未加载
评论 #8666747 未加载
评论 #8666459 未加载
评论 #8666430 未加载
评论 #8667191 未加载
评论 #8666810 未加载
评论 #8665416 未加载
jimmcslimover 10 years ago
I&#x27;m not sure whether to be heartened or concerned that the W3C is referencing the doge meme in its specifications... see Example 6.
评论 #8668269 未加载
lorddoigover 10 years ago
It amazes me that we&#x27;re now at the point of standardizing sticking array references inside strings and yet we&#x27;re still not having a serious discussion about what comes after HTML.
评论 #8665353 未加载
评论 #8665237 未加载
评论 #8665590 未加载
tomchristieover 10 years ago
Seems pretty decent. Also neat that the nesting style could be repurposed to support nested structures in regular form-encoded HTML forms.<p>Main limitation on <i>actually</i> being able to use this is that `GET` and `POST` continue to be the only supported methods in browser form submissions right now, so eg. you wouldn&#x27;t be able to make JSON `PUT` requests with this style anytime soon.<p>Might be that adoption of this would swing the consensus on supporting other HTTP methods in HTML forms.
tootieover 10 years ago
They&#x27;re still working on XForms after 10 years <a href="http://www.w3.org/MarkUp/Forms/" rel="nofollow">http:&#x2F;&#x2F;www.w3.org&#x2F;MarkUp&#x2F;Forms&#x2F;</a>
评论 #8665118 未加载
jdpover 10 years ago
The latest release of my jarg[0] utility supports the HTML JSON form syntax. Writing out JSON at the command line is tedious, this makes it a little nicer. The examples from the draft are compatible with jarg:<p><pre><code> $ jarg wow[such][deep][3][much][power][!]=Amaze {&quot;wow&quot;: {&quot;such&quot;: {&quot;deep&quot;: [null, null, null, {&quot;much&quot;: {&quot;power&quot;: {&quot;!&quot;: &quot;Amaze&quot;}}}]}}} </code></pre> [0]: <a href="http://jdp.github.io/jarg/" rel="nofollow">http:&#x2F;&#x2F;jdp.github.io&#x2F;jarg&#x2F;</a>
techtalskyover 10 years ago
Kind of nice, basically turns form submission into a bare-bones API call.
评论 #8665440 未加载
chronialover 10 years ago
Am I the only one who is worried about the fact that this is exponential in size?<p><pre><code> &lt;input name=&quot;field[1000000]&quot;&gt; </code></pre> Will generate a request that is ~5MB.
评论 #8668496 未加载
评论 #8667864 未加载
mnarayan01over 10 years ago
The JSON-based file upload would be nice (AFAIK there&#x27;s not great way to do this ATM, but I haven&#x27;t looked in over a year). The rest seems pretty weak-tea though. I can see multiple issues with more defined type (e.g. numeric rather than string values, null rather than blank string), but without dealing with that stuff, this seems of extremely limited utility.
评论 #8665412 未加载
stu_kover 10 years ago
Submitting files with this form encoding is of course going to have the base64 overhead, but otherwise this looks great!
评论 #8666721 未加载
评论 #8667347 未加载
评论 #8665229 未加载
pmontraover 10 years ago
A discussion about the implementation of the spec in jquery. It started on June 21<p><a href="https://github.com/macek/jquery-serialize-object/issues/24" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;macek&#x2F;jquery-serialize-object&#x2F;issues&#x2F;24</a>
homakovover 10 years ago
How is it solving CSRF JSON problem? <a href="http://homakov.blogspot.com/2012/06/x-www-form-urlencoded-vs-json-pros-and.html" rel="nofollow">http:&#x2F;&#x2F;homakov.blogspot.com&#x2F;2012&#x2F;06&#x2F;x-www-form-urlencoded-vs...</a>
billpgover 10 years ago
A new standard for referencing a point in a JSON object? I wonder if they considered RFC 6901 and rejected it.<p>I personally prefer this new square bracket notation, but being a standard already gets more points.
评论 #8666640 未加载
评论 #8666642 未加载
skratloover 10 years ago
Wow, W3C at it&#x27;s best again. Non-modular, non-negotiable, JSON it is, take it or leave it. Well fuck you W3C. Base64 encoded files? Seriously? What if my app workes better with msgpack encoded forms? Or with XML encoded? So you&#x27;re going to support one particular serialization format, quite a horrible one, but that&#x27;s subjective and that&#x27;s the whole point. Every app has different needs and you should spec. out a system that is modular and leaves the choice to the user, even for the price of &quot;complicating things&quot;.
评论 #8667752 未加载
评论 #8668373 未加载
edwinvdgraafover 10 years ago
Guessing that it&#x27;s interesting when using an uniform endpoint for both forms and js-driven requests.
Patrick_Devineover 10 years ago
Can we just get rid of HTML and replace it with JSON while we&#x27;re at it?
评论 #8666401 未加载
评论 #8665255 未加载
woutervdbover 10 years ago
&gt; wow[such][deep][3][much][power][!]<p>And there goes my interest for this submission. Don&#x27;t use overused memes in a submission. Liking the idea though.
评论 #8665271 未加载