Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Helidon MP 2.0 - How to use FormParams? #2405

Closed
ksurendra opened this issue Sep 30, 2020 · 3 comments
Closed

Question: Helidon MP 2.0 - How to use FormParams? #2405

ksurendra opened this issue Sep 30, 2020 · 3 comments

Comments

@ksurendra
Copy link
Contributor

How can we use Form Params on Helidon MP 2.0.2?

Environment Details

  • Helidon Version: 2.0.2
  • Helidon MP
  • JDK version: OpenJDK 11.0.2
  • OS: Mac OS 10.14.6
  • Docker version (if applicable): NA

Problem Description

If we have a form like this ( at /resources/webapp/index.html)

<body>
    <form action="/az/getUserProfile" method="post">
      <h1>Sample Login</h1>
      <div>
          <input type="text" placeholder="Username" required="" id="username" />
      </div>
      <div>
          <input type="password" placeholder="Password" required="" id="password" />
      </div>
      <div>
          <input type="submit" value="Log in" />
      </div>
  </form><!-- form -->
  </body>

and resource class:

...
@Path("/az")
@RequestScoped
public class AzResource {
   ...
   @Path("/getUserProfile") 
   @POST
   @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
   @Produces(MediaType.APPLICATION_JSON)
   public JsonObject getUserProfile() throws Exception {
      ...
      // if we can read "password" and "password"
      ...
      Form userform = new Form();
      userform.param("username", username)
            .param("password", password);
      .. // calling 3rd party api
}  

usually a typical way of reading them on jax-rs is:

import javax.ws.rs.FormParam;
...
public JsonObject getUserProfile(@FormParam("username") String username, @FormParam("password") String password) throws Exception {
   ...
}

References:

Steps to reproduce

@ksurendra
Copy link
Contributor Author

With help from @tjquinno on Slack, was able to resolve this.

The key is using id="username" did not work. Instead using name="username" and name="password" worked.

HTML:

<body>
    <form action="/azure/getUserProfile" method="post">
      <h1>Sample Login</h1>
      <div>
          <input type="text" placeholder="Username" required="" id="username" name="username"/>
      </div>
      <div>
          <input type="password" placeholder="Password" required="" id="password" name="password" />
      </div>
      <div>
          <input type="submit" value="Log in" />
      </div>
  </form><!-- form -->
  </body>

and Resource

import javax.ws.rs.FormParam;
....
  @Path("/getUserProfile")
  @POST
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.APPLICATION_JSON)
  public JsonObject getUserProfile(@FormParam("username") String username, @FormParam("password") String password) throws Exception {
    ...
    Form userform = new Form();
    userform.param("username", username)
            .param("password", password);
    ...
    Client client = ClientBuilder.newClient();
    String serverUrl = "https://login....../token";
    WebTarget target = client.target(serverUrl);
    final Invocation.Builder invocationBuilder = target.request();
    invocationBuilder.header("Content-Type", "application/x-www-form-urlencoded");
    final Response response = invocationBuilder.method(
                                                    HttpMethod.POST, 
                                                    Entity.entity(userform, MediaType.APPLICATION_FORM_URLENCODED), 
                                                    Response.class);
    String serverResponse = response.readEntity(String.class);
    ...
    return jsonObject;
}

@tjquinno
Copy link
Member

The JavaDoc for the @FormParam value attribute uses the term name extensively and never mentions id. There might be JAX-RS doc somewhere that states this more definitively.

@spericas
Copy link
Member

spericas commented Oct 1, 2020

Attribute id should really be about uniquely identifying a node in a DOM, such as for CSS or JS. It of course needs to be unique. I think using name should be used for form posting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants