Re: [Vala] Looking for an example on how to get the data from a web form (when using a Soup-based server)



Couldn't find anything, so I just wrote a small app to do that. Probably
has a few bugs, but it's only to show the idea. Also pasted at
http://pastebin.com/6U2JGmuh


   1.  void default_handler (Soup.Server server, Soup.Message msg, string
    path,
   2.                       GLib.HashTable<string,string?>? query, Soup.
   ClientContext client)
   3.  {
   4.   var form_data = query;
   5.    if (msg.method == "POST") {
   6.     msg.request_body.flatten();
   7.      form_data = Soup.Form.decode((string) msg.request_body.data);
   8.   }
   9.    var user = (form_data != null) ? form_data.get("user") : "";
   10.
   11.    var welcome = "";
   12.   if (user != null)
   13.      welcome = "<p>Welcome %s</p>".printf(user);
   14.
   15.    var data = new HashTable<string, string>(str_hash, str_equal);
   16.   data.set("$user", user);
   17.    data.set("$welcome", welcome);
   18.   data.set("$path", path);
   19.
   20.   string response_text = """
   21.     <html>
   22.       <body>
   23.         <p>Current location: $path</p>
   24.         $welcome
   25.         <form name='input' action='$path' method='get'>
   26.           Name: <input type='text' name='user' value='$user' />
   27.         <input type='submit' value='Submit GET' />
   28.         </form>
   29.         <form name='input' action='$path' method='post'>
   30.           Name: <input type='text' name='user' value='$user' />
   31.         <input type='submit' value='Submit POST'/>
   32.         </form>
   33.       </body>
   34.     </html>""";
   35.
   36.   data.foreach((k, v) => {
   37.      response_text = response_text.replace(k, v);
   38.   });
   39.
   40.   msg.set_response ("text/html", Soup.MemoryUse.COPY,
   41.                      response_text.data);
   42.  }
   43.
   44. int main (string[] args) {
   45.      var port = 8088;
   46.     var server = new Soup.Server (Soup.SERVER_PORT, port);
   47.      server.add_handler ("/", default_handler);
   48.     stdout.printf("Serving on http://localhost:%d\n";, port);
   49.      server.run ();
   50.
   51.      return 0;
   52.  }


*Alexandre Rosenfeld*

On Sat, Nov 12, 2011 at 21:50, Serge Hulne <serge hulne gmail com> wrote:

Hi,

I am looking for a slightly more complete example than the one from the
Vala tutorials on "Soup".

More particularly, I am looking for a snippet that would show how to get
(and possibly decode) the data sent via HTTP from an HTML web form (in a
browser) to a (Soup-based) server.

Thanks !

Serge.

_______________________________________________
vala-list mailing list
vala-list gnome org
http://mail.gnome.org/mailman/listinfo/vala-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]