17

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>
Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38

5 Answers5

20

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

request.queryParams("userName")
Stephan
  • 41,764
  • 65
  • 238
  • 329
  • 13
    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. – bric3 Feb 10 '16 at 13:48
  • 1
    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? – Raf Jun 08 '16 at 07:03
  • 2
    @Raf **Is this normal behaviour of java spark?** You name it: this is a *weird* behaviour of java spark. – Stephan Jun 08 '16 at 15:08
  • @Stephan 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 http://stackoverflow.com/questions/37715875/testing-java-spark-microservices-app-that-implements-sparkapplication-interface , your feedback would be appreciated. – Raf Jun 09 '16 at 02:31
  • As mentioned by @Raf 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. – AniJ Jun 24 '18 at 11:52
  • 1
    `queryParams` delegates to the underlying HTTP infrastructure's `Request::getParameter` (see https://github.com/perwendel/spark/blob/master/src/main/java/spark/Request.java#L292). 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." https://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getParameter-java.lang.String- – Jeremy Wiebe Jul 04 '19 at 00:22
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.

Community
  • 1
  • 1
Pankaj
  • 302
  • 4
  • 7
3

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

testinfected
  • 306
  • 1
  • 4
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);
}
Hanson
  • 254
  • 2
  • 10
-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.

Laercio Metzner
  • 1,351
  • 11
  • 22