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>