Evgeny Kuznetsov
Evgeny Kuznetsov

Reputation: 21

Required request parameter for method POST is not present

I am struggling to solve an issue with @RequestParam in a POST method in a Spring Boot application. I have a simple form on the page with only one parameter:

@GetMapping("/")
public String mainPage(Model model){
     return "HelloPage";
}

And HelloPage for it:

<div class="form-group col-sm-6">
  <form method="post" enctype="text/plain">
      <div class="form-group">
          <label>
              <input type="text" class="form-control"
                     name="authorname" placeholder="Employee name"
              />
          </label>
      </div>
      <div class="form-group">
          <button type="submit" class="btn btn-primary ml-2">Next</button>
      </div>
  </form>
</div>

I created a POST method to create a new author and redirect to another page where I want to show this author name:

@PostMapping("/")
public String postAuthor(@RequestParam("authorname") String authorname){
  Author author = authorService.saveAuthor(authorname);
  return "redirect:/surveys/" + author.getId();
}

When I click on the button after filling the form on HelloPage it gives me this error:

There was an unexpected error (type=Bad Request, status=400). Required request parameter 'authorname' for method parameter type String is not present org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'authorname' for method parameter type String is not present

I do not understand why this is happening since the POST method should be able to get request parameters from the form!

Author is just a simple entity model:

@Entity
@Table(name = "authors")
public class Author {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String authorname;

    public Author() {}

    public Author(String authorname) {
        this.authorname = authorname;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAuthorname() {
        return authorname;
    }

    public void setAuthorname(String authorname) {
        this.authorname = authorname==null || authorname.isEmpty()? "default user" : authorname;
    }
}

Can anyone explain and help me what is wrong here please?

Upvotes: 0

Views: 1619

Answers (1)

Jo&#227;o Dias
Jo&#227;o Dias

Reputation: 17460

Try removing enctype="text/plain". According to https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#plain-text-form-data:

Payloads using the text/plain format are intended to be human readable. They are not reliably interpretable by computer, as the format is ambiguous (for example, there is no way to distinguish a literal newline in a value from the newline at the end of the value).

Having said that, try the following:

<div class="form-group col-sm-6">
  <form method="post">
      <div class="form-group">
          <label>
              <input type="text" class="form-control"
                     name="authorname" placeholder="Employee name"
              />
          </label>
      </div>
      <div class="form-group">
          <button type="submit" class="btn btn-primary ml-2">Next</button>
      </div>
  </form>
</div>

Upvotes: 1

Related Questions