Search code examples
javaerror-handlinghttp-posthtmlunit

How to use getPage() on a request with a request body?


At work, I am using HtmlUnit version 2.8, and I've come to this kafkaian situation:

I have a WebRequest that has a body, thus no request parameters: using WebRequest.setRequestParameters() when the request body is not null throws a:

java.lang.RuntimeException: Trying to set the request parameters, but the request body has already been specified;the two are mutually exclusive!

(source available here)

However, when I use webClient.getPage(webRequest), I get:

java.lang.NullPointerException: parameters

(source here)

That really verbose message meaning that it's not OK to have null for request parameters.

Is there a way to fix this, or bypass it? Preferably using same version (again, 2.8), as I don't have the rights to do upgrade the frameworks we use.


Solution

  • Well the default constructor initializes by default the request's parameters to Collections.emptyList(), so it will not cause the NullPointerException.

    What did cause my exception though, was that a few lines before I called

    request.setRequestParameters(null);
    

    which obviously was the cause to my issue.

    I removed that line and now it works just as expected.