helidon icon indicating copy to clipboard operation
helidon copied to clipboard

[4.x] form params media example

Open romain-grecourt opened this issue 10 months ago • 1 comments

Environment Details

  • Helidon Version: 4.0.7
  • Helidon SE

Problem Description

There is no example that demonstrates how to process form parameters (both client side and server side).

Steps to reproduce

See a self contained test for inspiration:

@ServerTest
class FormTest {

    private final Http1Client client;

    FormTest(Http1Client client) {
        this.client = client;
    }

    @SetUpServer
    private static void setup(WebServerConfig.Builder server) {
        server.routing(r -> r.post((req, res) -> {
            var params = req.content().as(Parameters.class);
            res.send(params);
        }));
    }

    @Test
    void testFormSubmit() {
        var entity = Parameters.create("test", Map.of("foo", List.of("bar")));
        var response = client.post().submit(entity, String.class);
        assertThat(response.status(), is(Status.OK_200));
        assertThat(response.entity(), is("foo=bar"));
    }
}

romain-grecourt avatar Apr 11 '24 23:04 romain-grecourt

It's probably worth having an example that also needs to set some headers. Both with and without shortcut methods.

For example, this is a request being made to the Steam API which makes use of shortcut methods and generic header() calls.

WebClient client = WebClient.create();
HttpClientRequest req = client.post("https://steamcommunity.com/openid/login");
req.contentType(MediaTypes.APPLICATION_FORM_URLENCODED);
req.header(HeaderNames.ACCEPT_LANGUAGE, "en");
req.header(HeaderNames.CONTENT_LENGTH, Integer.toString(payload.length()));
req.followRedirects(false);

paul-court avatar Apr 11 '24 23:04 paul-court