Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post html form to restlet

Tags:

java

html

restlet

I have an html form that I need to submit to a restlet. Seems simple enough but the the form always comes back empty.

This is the form:

<form action="/myrestlet" method="post"> 
    <input type="text" size=50 value=5/>
    <input type="text" size=50 value=C:\Temp/>
    (and a few other input type texts)
</form>

The restlet:

@Post
public Representation post(Representation representation) {
    Form form = getRequest().getResourceRef().getQueryAsForm();
    System.out.println("form " + form);
    System.out.println("form size " + form.size());
}

I also tried getting for form like this:

Form form = new Form(representation);

But it always comes as [] with size 0.

What am I missing?

EDIT: Here's the workaround I'm using:

String query = getRequest().getEntity().getText();

This has all the values from the form. I have to parse them, which is annoying, but it does the job.

like image 678
Eddy Avatar asked May 14 '26 03:05

Eddy


2 Answers

Here is the correct way to get values from a submitted HTML form (with content type application/x-www-form-urlencoded) within a Restlet server resource. It's what you did in fact.

public class MyServerResource extends ServerResource {
    @Post
    public Representation handleForm(Representation entity) {
        Form form = new Form(entity);

        // The form contains input with names "user" and "password"
        String user = form.getFirstValue("user");
        String password = form.getFirstValue("password");

        (...)
    }
}

In your case, the HTML form isn't actually sent because you didn't define any attribute name for your form. I used your HTML code and the sent data is empty. You can check this using the Chrome developer tools (Chrome) or Firebug (Firefox).

POST /myrestlet HTTP/1.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Connection: keep-alive
Host: localhost:8182
Referer: http://localhost:8182/static/test.html
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
Content-Length: 0
Content-Type: application/x-www-form-urlencoded

You should use something like that for your HTML form:

<form action="/test" method="post">
  <input type="text" name="val1" size="50" value="5"/>
  <input type="text" name="val2" size="50" value="C:\Temp"/>
  (and a few other input type texts)
  <input type="submit" value="send">
</form>

In the case, the request would be:

POST /myrestlet HTTP/1.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Connection: keep-alive
Host: localhost:8182
Referer: http://localhost:8182/static/test.html
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0
Content-Length: 23
Content-Type: application/x-www-form-urlencoded

val1=5&val2=C%3A%5CTemp

Hope it helps you, Thierry

like image 58
Thierry Templier Avatar answered May 15 '26 17:05

Thierry Templier


here is a little bit simpler way to achieve this, it directly declares the Form as parameter of the Java method:

public class MyServerResource extends ServerResource {
    @Post
    public Representation handleForm(Form form) {

        // The form contains input with names "user" and "password"
        String user = form.getFirstValue("user");
        String password = form.getFirstValue("password");

    (...)
    }
}
like image 37
Thierry Boileau Avatar answered May 15 '26 16:05

Thierry Boileau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!