How to get data from form with spark java?
Asked Answered
M

5

18

I'm pretty new with all these things but hope that you guys can help me understand how does it work. I got a form with field . How do i get data from client back? Was looking for some information but couldnt find.

<form action="Dispatcher" method="post">
    <fieldset>
        <p>Name</p>
        <input type="text" name="userName" required="true">

        <p>Email</p>
        <input type="text" name="userEmail" required="true">
        <input type="submit" value="submit">
    </fieldset>
</form>
Measures answered 28/3, 2015 at 0:1 Comment(1)
Did any of the answers work for you?Creolized
N
21

I faced the same issue. I used queryParams to solve it:

request.queryParams("userName")
Nylon answered 10/9, 2015 at 1:2 Comment(7)
Note you have to ensure that request.body() is not invoked beforehand (for logging purpose for example), otherwise it consumes the body, the form param won't be parsed. The javadoc is quite lacking on that matter.Windhover
This is weird. I was expecting request.params("userName") to return the value but instead request.queryParams("userName") gives the same value. I thought queryParams was supposed to return query strings. Is this normal behaviour of java spark?Rosado
@Rosado Is this normal behaviour of java spark? You name it: this is a weird behaviour of java spark.Nylon
@Nylon thanks, your answer saved me. I wanted to test microservice and I have done it kind of differently than most tests example available online, can you please have a look at this question #37716375 , your feedback would be appreciated.Rosado
As mentioned by @Rosado earlier I expected request.params("userName") to work as well. But request.queryParams("userName") has actually worked for me and I do not understand why. If anyone can explain please add. A piece if additional information I am testing this form submission using Postman and only "x-www-form-urlencoded" request work. Post requests set to "form-data" on Postman do not work.Antalya
queryParams delegates to the underlying HTTP infrastructure's Request::getParameter (see github.com/perwendel/spark/blob/master/src/main/java/spark/…). That methods docs explain "Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data." docs.oracle.com/javaee/7/api/javax/servlet/…Inexplicable
This is the correct method to use — even if the function's name, the Spark documentation (sparkjava.com/documentation#request) and the code comment suggest that it will only return query parameters. The method's documentation (docs.oracle.com/javaee%2F6%2Fapi%2F%2F/javax/servlet/…), however, is definitive in that it also returns POST parameters as well.Windswept
D
5

Just for the record, I faced a similar problem and this how I solved it.

Since it is a multipart request, you will have to specify that

MultipartConfigElement multipartConfigElement = new MultipartConfigElement(path);

req.raw().setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement);

You can find the details here SparkJava: Upload file did't work in Spark java framework

Once this is done correctly you should be able to access the data using query params as mentioned Stephan.

Detumescence answered 24/6, 2016 at 1:20 Comment(0)
P
3

I think you need to use request.params("userName") which will give you the list of parameters submitted with name userName

Pawnbroker answered 28/3, 2015 at 0:31 Comment(1)
Weird naming convention. Should have made it much more obvious: getPostParamAtion
S
1

As Java doc says,

If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputStream() or getReader() can interfere with the execution of this method.

So request.queryParams("userName") won't work after calling request.body() (for logging for instance). I wrote a utility function to get parameter.

public static String getParameter(String body, String param) {
    HashMap<String, String> params = new HashMap();
    for (String s : body.split("&")) {
        String[] kv = s.split("=");
        params.put(kv[0], kv[1]);
    }
    String encoded = params.get(param);
    return URLDecoder.decode(encoded, StandardCharsets.UTF_8);
}
Skewbald answered 21/10, 2019 at 4:34 Comment(0)
G
-3

I think you'd better use a js framework like AngularJS or JQuery to convert form data into json before sending it to the server.

Gerrard answered 18/12, 2015 at 0:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.