About this lesson
REST Assured formParam: POST x-www-form-urlencoded data like an HTML form
Learn REST AssuredAdvancedAdvanced Topics
How it works
Before JSON, web forms posted their fields as application/x-www-form-urlencoded: a single body of name=value pairs joined by ampersands, with special characters percent-encoded. Many login, OAuth token and legacy endpoints still expect it. In REST Assured each field is one formParam(name, value) call, and formParams() takes a Map when you have several.
Content type matters to the server. testkru's /form endpoint rejects anything else with 415 and an error naming the type it received; posting JSON there returned exactly that. Setting the type with contentType() makes the intent explicit, but REST Assured also sets a form content type on its own when form parameters are present and you have not chosen one, and that request was accepted too. The testkru login endpoint follows the same parsing rules and accepts form-encoded credentials.
The response has two parts. received is an object that echoes your fields by name. contentType is a fixed string written into the server code, not a copy of your header, so it does not prove what you sent. The real proof is that you got a 200 rather than a 415.
About the assignment
Post two form fields to the form endpoint and print the content type it reports and the two values it received.
What you will do
- Start from
given(), set the content type to the form type named in the starter comment, and addnameandcitywith oneformParam()call each, using the values from the comment. - Send the request with
post()to/formand keep theResponse. - Read
contentType,received.nameandreceived.citywithjsonPath().getString(). - Print three labelled lines in the order of the expected output:
Content Type:,Name:andCity:.
Common mistakes
Sending a JSON body
Using
body()with a JSON string and the JSON content type, as in earlier POST lessons, gets a 415 from this endpoint. Form endpoints need form parameters.Reading the fields at the root
The echoed values sit inside
received. A path of plainnamereturnsnull, so the label prints with null after it.Trusting the contentType field
It is the same string on every successful response. If you want to check the request side, assert on the status code, which is where a wrong type would show up.
In a real test suite
OAuth 2.0 token endpoints are the most common place testers meet form encoding: client credentials and grant type are posted as form fields, and the token comes back as JSON. A JUnit 5 helper that posts those fields with formParam() and returns the extracted token is a typical first utility in an API test project.
Related lessons
- Create a User with POSTPosts a JSON body with
ContentType.JSON, the format this lesson replaces. - API Workflow - Authentication and OrderLogs in with credentials before using the token in a second call.
- Validate Content-Type with MatchersValidates the
Content-Typeof a response with theContentTypeenum.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: POST to /form with Content-Type application/x-www-form-urlencoded
// Use .formParam("name", "John") and .formParam("city", "New York")
// TODO: Print contentType, received.name, and received.city from the response
}
}