helidon-io / helidon

Java libraries for writing microservices
https://helidon.io
Apache License 2.0
3.49k stars 563 forks source link

[4.x] form params media example #8652

Open romain-grecourt opened 5 months ago

romain-grecourt commented 5 months ago

Environment Details


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"));
    }
}
gargoyle commented 5 months ago

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);