top of page
Search

How to request application form encoded in restAssured

  • Writer: Dev Raj Sinha
    Dev Raj Sinha
  • Jun 18, 2023
  • 1 min read

To send a form-urlencoded request using RestAssured, you can use the `formParams()` method to specify the form parameters and the `contentType()` method to set the request content type to `application/x-www-form-urlencoded`. Here's an example of how to request an application form-encoded using RestAssured:


import io.restassured.RestAssured; 
import io.restassured.response.Response;  
Response response= RestAssured.given()     .contentType("application/x-www-form-urlencoded")     .formParams("param1", "value1", "param2", "value2")     .post("https://api.example.com/endpoint");
    

In this example, we use the `given()` method to set up the request specification. We specify the content type of the request as `application/x-www-form-urlencoded` using the `contentType()` method. The `formParams()` method is used to specify the form parameters, where each parameter is passed as a key-value pair. In this case, we're passing two form parameters: `param1` with a value of `"value1"` and `param2` with a value of `"value2"`. Finally, we use the `post()` method to send a POST request to the specified endpoint.


RestAssured will automatically encode the form parameters and include them in the request body with the appropriate content type header.


Note that you can also use the `put()`, `get()`, or other HTTP methods instead of `post()` based on the specific requirements of your API.


By using the `formParams()` method and setting the content type to `application/x-www-form-urlencoded`, you can send form-urlencoded requests using RestAssured. This is useful when interacting with APIs that expect form data, such as submitting HTML forms or working with OAuth flows that require form-encoded parameters.

 
 
 

Recent Posts

See All

Comments


Never Miss a Post. Subscribe Now!

Find new exciting ways to dominate the IT Automation industry

Thanks for submitting!

bottom of page