2

What is the right way to read form parameters in helidon-se.

I have tried doing the following without success.

  1. Add DefaultMediaSupport.formParamReader() to the webserver:
WebServer.builder()
 .addReader(...) // added here
 .build()

Then reading the request::content with

request.content()
  .as(FormParams.class)
  .thenAccept(...) // never called
  1. Read directly from the Request::content:
DefaultMediaSupport.formParamReader()
  .unmarshall(..., FormParams.class)
  .thenAccept(...) // never called

Both cases the client (browser/httpie) times out after 30s and the request isn't handled.

So, what is the right way to read form parameters from a request body in helidon-se.

Thanks.

Harry nc-d
  • 61
  • 4

1 Answers1

3

The reader for FormParams is available out-of-the-box, you don't have to explicitly register it (unless you've disabled the default readers). It only supports two content types:

  • application/x-www-form-urlencoded
  • text/plain

You can use it like this:

req.content().as(FormParams.class).thenAccept(fp -> {
    fp.first("foo")
      .ifPresentOrElse((greeting) -> res.send(greeting),
              () -> res.status(400).send());
});

You can test this with curl:

curl -X POST -d 'foo=bar' http://localhost:8080/abc

In HTML the form would look like this:

<form action="/abc" method="post" enctype="application/x-www-form-urlencoded">
    <input type="text" name="foo" />
    <input type="submit" value="Submit!" name="submit">
</form>

Helidon also has support for multipart/form-data:

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.helidon.media</groupId>
    <artifactId>helidon-media-multipart</artifactId>
</dependency>

Configure the media support in the WebServer builder:

WebServer.builder(createRouting())
         .addMediaSupport(MultiPartSupport.create())

Then use it like this:

req.content().as(ReadableMultiPart.class).thenAccept(multiPart -> {
    multiPart.field("foo")
             .ifPresentOrElse((part) -> res.send(part.as(String.class)),
                     () -> res.status(400).send());
});

You can test it with curl like this:

curl -X POST -F 'foo=bar' http://localhost:8080/abc

In HTML the form would look like this:

<form action="/abc" method="post" enctype="multipart/form-data">
    <input type="text" name="foo" />
    <input type="submit" value="Submit!" name="submit">
</form>
Romain Grecourt
  • 488
  • 2
  • 4