Basic JSoup form Submission

Tags: ,



My form submission does not seem to work, I tried JAunt it was able to submit so when using JSoup I don’t understand why it returns 404.

Connection.Response response = Jsoup.connect("https://crawlertest284814019.wordpress.com/contact/#contact-form-7")
    .userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
    .data("name", "nameeee")
    .method(Connection.Method.POST)
    .execute();
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.13.1</version>
</dependency>

I tried with:

  • url https://crawlertest284814019.wordpress.com/contact/
  • data "name", "nameeee" produces 404 status
  • data "g7-name", "nameeee" no issue but no submission
  • data "Name", "nameeee" no issue but no submission
  • data with Map<String, String> no issue but no submission

Answer

As pointed out by @Pshemo, the code is ok however the data passed is lacking. Those hidden fields contain information required in order for the submission to be valid. In the case of this question the hidden fields are:

<input type="hidden" id="_wpnonce" name="_wpnonce" value="4d18e01372">
<input type="hidden" name="_wp_http_referer" value="/contact/">
<input type="hidden" name="contact-form-id" value="7">
<input type="hidden" name="action" value="grunion-contact-form">
<input type="hidden" name="contact-form-hash" value="07cbf543790375cf26022ad8826a8dbb8787beae">

So aside from the required fields make sure to submit the hidden once as well.

Document document = Jsoup.connect("https://crawlertest284814019.wordpress.com/contact/#contact-form-7")
    .data("g7-name", "Super sName")
    .data("_wpnonce", "4d18e01372")
    .data("_wp_http_referer", "/contact/")
    .data("action", "grunion-contact-form")
    .data("contact-form-id", "7")
    .data("contact-form-hash", "07cbf543790375cf26022ad8826a8dbb8787beae")
    .post();


Source: stackoverflow