3

Unlike GET, in which building the query is straightforward (concatenating fields into one long URL), I seem to be missing the "secret" of generating a POST.

I know that the URL provided in the <FORM ACTION="https://host.domain.com/etc"> should be used in some way, but how?

The tutorials I found on this subject remain mysterious about this for some reason.

How do I build an HTML POST?

Is that a straightforward URL string loaded into the browser? or is there more to it?

Is the answer is browser/platform dependent, I am using WebView on Android.

Community
  • 1
  • 1
ateiob
  • 9,016
  • 10
  • 44
  • 55
  • The data from the form are sent as the entity-body of an HTTP POST request with content-type `application/x-www-form-urlencoded`. If that is Greek to you (which I suspect it will be), go read through some introductory documentation on HTTP, and then ask again if you're still unsure. – hmakholm left over Monica Aug 26 '11 at 21:53
  • @Henning Makholm Thanks. There is so much material on HTTP... If you can recommend a particular source for such introductory documentation on HTTP, that would be super. I don't intend to get a PhD in HTTP, only implement a tiny Java method that posts 2 fields. I now suspect [HttpPost](http://developer.android.com/reference/org/apache/http/client/methods/HttpPost.html) will do the magic but your idea of getting an intro on this is great. Any recommendations? – ateiob Aug 26 '11 at 21:59
  • Well, my own preferred strategy in such cases is to go directly to [the spec itself](http://www.w3.org/Protocols/rfc2616/rfc2616.html) and read through it from beginning to end a few times, hoping that it will all make sense the second or third time around. But that doesn't seem to work well for most other people ... – hmakholm left over Monica Aug 26 '11 at 22:11
  • 1
    Thanks. Here is the definition of [application/x-www-form-urlencoded](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1) – ateiob Aug 26 '11 at 22:14

1 Answers1

4

Well, if you're working with HTML, it should be the same way that you would do it on the browser.

That is, add a method attribute to your form tag and if you're working with files a enctype attribute.

<form method="post" enctype="multipart/form-data"><!--values here--></form>

I suggest you read the wikipedia page on POST: http://en.wikipedia.org/wiki/POST_(HTTP) Basically, instead of encoding the data in the url it sends it in a message body. Also see: http://www.cs.tut.fi/~jkorpela/forms/methods.html

Beside it 'hides' the query data from the user, it is used for uploading files because you can include binary data and not just text.

This is useful if you want to keep your urls clean when working with forms. (For example, you include a long string in your form data the url will get really long.)

Also, the browser will usually prompt the user when they refresh a form page. The browser won't do this for GET, this is usually used for things like navigation or for passing parameters to the server that the user might find useful to change themselves directly. (i.e. page_id=1 and the user can just go to the next page or a specific page by changing a single number.)

Miguel Morales
  • 1,707
  • 10
  • 10
  • [This](http://www.cs.tut.fi/~jkorpela/forms/methods.html#tech) is exactly what I needed. Thank you! – ateiob Aug 26 '11 at 22:04
  • 1
    And [this](http://stackoverflow.com/questions/3711126/open-webview-with-result-from-http-post/3711251#3711251) code sample seems to be an icing on the cake. – ateiob Aug 26 '11 at 22:20
  • 1
    Another [relevant one](http://stackoverflow.com/questions/3471581/android-webview-post/3472052#3472052). – ateiob Aug 26 '11 at 22:23