Skip to content
CodeKruPro

Form URL Encoded Data

60/62

Form URL Encoded Data

Not all APIs accept JSON. Some use application/x-www-form-urlencoded (like HTML forms). Use .formParam():

given()
    .contentType("application/x-www-form-urlencoded")
    .formParam("field", "value")  // one call per form field
    .post(path);

Task: Send form data and verify the server received it.

Expected output:

Content Type: application/x-www-form-urlencoded
Name: John
City: New York

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

  1. Start from given(), set the content type to the form type named in the starter comment, and add name and city with one formParam() call each, using the values from the comment.
  2. Send the request with post() to /form and keep the Response.
  3. Read contentType, received.name and received.city with jsonPath().getString().
  4. Print three labelled lines in the order of the expected output: Content Type:, Name: and City:.

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 plain name returns null, 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.

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.

Main.java
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
    }
}