Search code examples
javarestrest-assuredrest-assured-jsonpath

In Java Rest Assured framework is there any way to use x-www-form-urlencoded form params using POJO classes?


In Rest Assured framework we use POJO classes concept when there is a JSON payload. But right now I am having x-www-form-urlencoded form params. Is there any way we can use POJO classes for x-www-form-urlencoded form params? Please let me know the better way to handle x-www-form-urlencoded form params?

Currently I am handling in the below way.

.header("Content-Type", "application/x-www-form-urlencoded");
.formParam("i_username",username);
.formParam("i_password",password);

Solution

  • You can use Map<String, ?> to provide values for formParams.

    RequestSpecification formParams(Map<String, ?> parametersMap);
    

    For example:

    Map<String, Object> body = new HashMap<>();
    body.put("i_username",username);
    body.put("i_password",password);
    
    given().header("Content-Type", "application/x-www-form-urlencoded")
           .formParams(body);
    

    And if you still want to use POJO, you can write a small method to convert POJO to Map, then do it as the same above instruction.