Post

Send Query and Form Parameters in REST Assured

Send query parameters, multi-value and empty parameters, java.time values and form bodies, control URL encoding, and learn where the catch-all param(..) puts its value.

Send Query and Form Parameters in REST Assured

Your payment search test filters on status and currency, and it passes, but it would also pass if the filter were ignored: a param(..) on a POST quietly moved the values into the body. A second test sends the name Anna de Vries & Co, and the server sees a parameter called ` Co`. In this guide, you’ll send query and form parameters on purpose, and check on the wire where each value ended up.

Level: Beginner · Time: 25 minutes · Part of: Module 2: Building Requests, section 2

What You’ll Build

You’ll build a test class called ParametersTest with 14 tests. They send query parameters, form bodies and the catch-all param(..) to the echo endpoint, and prove what REST Assured put in the URL and the body. Two tests use real endpoints: a payment search and a beneficiary form.

One of the tests sends this request and checks this response:

1
2
3
4
5
6
7
GET /api/v1/echo?status=RECEIVED&status=CANCELLED HTTP/1.1
Host: localhost:<port>

HTTP/1.1 200 OK
Content-type: application/json; charset=utf-8

{"rawQuery":"status=RECEIVED&status=CANCELLED","query":{"status":["RECEIVED","CANCELLED"]}, ...}

When you’re done, Maven reports 14 passing tests:

1
2
[INFO] Tests run: 14, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

In this guide, you will:

  • Send query parameters.
  • Send flags and dates as query parameters.
  • Control URL encoding.
  • Send a form body.
  • Know where param(..) puts its value.

What You Need

How to Complete This Guide

You can work through this guide in one of two ways:

  • Build it step by step: create ParametersTest in a new section_02 package next to the class from the previous section, and follow every heading below.
  • Skip ahead: open the finished class (ParametersTest.java) and read along. You can run it at any time with the command in Run the Tests.

Get the code: if you haven’t cloned the examples repository yet, follow the steps in Fill Path Parameters in REST Assured.

Out of scope: this guide doesn’t cover the charset REST Assured adds to a form’s content type, or JSON bodies; they’re taught in section 3 and section 4.

Understand Query and Form Parameters

The problem. Query parameters and form fields are both name and value pairs, so it’s tempting to treat them alike. They travel in different places, though: the query string is part of the URL, and a form is the request body. A value in the wrong place, or one whose & wasn’t encoded, reaches the server as something else, and a test that only checks the status code won’t notice.

The concept. REST Assured has three methods for name and value pairs:

MethodWhere the value goes
queryParam(..)Always in the URL, after the ?
formParam(..)Always in the body, as application/x-www-form-urlencoded
param(..)The query string for GET; a form body for POST without another body

Each has a plural form that takes a map: queryParams(..), formParams(..) and params(..). REST Assured URL-encodes every value unless you turn encoding off.

When to use each. Use queryParam(..) for filters, searches and flags, and formParam(..) for endpoints that accept HTML-style forms. Avoid param(..) in tests: it works, but the reader must know the rule to see where the value went.

Say where a value goes: use queryParam(..) or formParam(..), not param(..).

Meet the API Under Test

This guide uses three endpoints of the Payments API, under /api/v1:

MethodPathPurpose
Any/echoDescribe the request as the server received it
GET/paymentsList payments, filtered by status (repeatable) and currency
POST/beneficiariesCreate a beneficiary from a form with name, iban and an optional nickname

A request to /api/v1/echo?status=RECEIVED&status=CANCELLED&verbose returns a response similar to the following, shortened:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
HTTP/1.1 200 OK
Content-type: application/json; charset=utf-8

{
    "method": "GET",
    "path": "/api/v1/echo",
    "rawPath": "/api/v1/echo",
    "rawQuery": "status=RECEIVED&status=CANCELLED&verbose",
    "query": {
        "status": ["RECEIVED", "CANCELLED"],
        "verbose": [""]
    },
    "headers": { ... },
    "contentType": "",
    "bodyLength": 0,
    "body": ""
}

A form sent to POST /beneficiaries returns 201 Created:

1
2
3
4
5
6
7
8
9
HTTP/1.1 201 Created
Content-type: application/json; charset=utf-8
Location: /api/v1/beneficiaries/ben-1

{
    "id": "ben-1",
    "name": "Jan & Piet",
    "iban": "NL69INGB0123456789"
}

Your tests check these echo fields:

  • rawQuery, the query string exactly as it was sent. It’s "" when the URL has no query.
  • query, the decoded query parameters, as a map from each name to the list of its values.
  • body, bodyLength and contentType, which show what was sent in the body.

Send Query Parameters

Set Up the Test Class

Goal: create the test class, start the Payments API and add the shared request helper.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.javanibble.module_02.section_02;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.startsWith;

import com.javanibble.payments.PaymentsApi;

import io.restassured.specification.RequestSpecification;

import java.time.LocalDate;
import java.util.Map;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class ParametersTest {

    private static PaymentsApi api;

    @BeforeAll
    static void startApi() {
        api = PaymentsApi.start();
    }

    @AfterAll
    static void stopApi() {
        api.close();
    }

    private static RequestSpecification paymentsApi() {
        return given().baseUri(api.baseUri()).port(api.port()).basePath(PaymentsApi.BASE_PATH);
    }
}

What’s happening:

  • The lifecycle methods and the paymentsApi() helper are the same as in the previous section.
  • contains(..), everyItem(..), hasKey(..) and startsWith(..) are new matchers, explained where they’re used.

Checkpoint: the class compiles. There are no tests yet, so there’s nothing to run until the next step.

Send One Parameter

Goal: add one query parameter and check it in both the raw and decoded form.

1
2
3
4
5
6
7
8
9
10
@Test
void oneQueryParameter() {
    paymentsApi()
        .queryParam("status", "RECEIVED")
    .when()
        .get("/echo")
    .then()
        .body("rawQuery", equalTo("status=RECEIVED"))
        .body("query.status", contains("RECEIVED"));
}

What’s happening:

  • queryParam("status", "RECEIVED") adds ?status=RECEIVED to the URL.
  • rawQuery proves what was sent, and query.status what the server decoded.
  • query.status is a list, even for one value, so contains("RECEIVED") checks that the list holds exactly that one element.

Checkpoint: run ParametersTest from your IDE. One test runs and passes.

Don’t compare a multi-value field with equalTo(..). equalTo("RECEIVED") fails against the list [RECEIVED], as you’ll see in Make a Test Fail. Use contains(..) or hasItem(..).

Send Several Parameters From a Map

Goal: send two query parameters at once.

1
2
3
4
5
6
7
8
9
10
@Test
void severalQueryParametersFromAMap() {
    paymentsApi()
        .queryParams(Map.of("status", "RECEIVED", "currency", "EUR"))
    .when()
        .get("/echo")
    .then()
        .body("query.status", contains("RECEIVED"))
        .body("query.currency", contains("EUR"));
}

What’s happening:

  • queryParams(..) sends every map entry as a parameter.
  • Map.of(..) has no fixed order, so the test checks the decoded query map, not rawQuery, whose order could change.

Checkpoint: run the class again. Two tests pass.

Send Several Values for One Name

Goal: send two statuses under one parameter name.

1
2
3
4
5
6
7
8
9
10
@Test
void aMultiValueQueryParameterRepeatsTheName() {
    paymentsApi()
        .queryParam("status", "RECEIVED", "CANCELLED")
    .when()
        .get("/echo")
    .then()
        .body("rawQuery", equalTo("status=RECEIVED&status=CANCELLED"))
        .body("query.status", contains("RECEIVED", "CANCELLED"));
}

What’s happening:

  • Extra arguments to queryParam(..) are extra values. REST Assured repeats the name for each one.
  • contains("RECEIVED", "CANCELLED") checks every element, in order.
  • This is how GET /payments expects several statuses.

Checkpoint: run the class again. Three tests pass.

Some APIs want status=RECEIVED,CANCELLED instead of a repeated name. For those, join the values yourself and pass one string.

Check Every Value

Goal: check that every value of a parameter matches, however many there are.

1
2
3
4
5
6
7
8
9
@Test
void everyValueOfAQueryParameterIsChecked() {
    paymentsApi()
        .queryParam("status", "RECEIVED", "RECEIVED")
    .when()
        .get("/echo")
    .then()
        .body("query.status", everyItem(equalTo("RECEIVED")));
}

What’s happening:

  • everyItem(..) applies a matcher to each element, and unlike contains(..), it doesn’t care how many there are.

Checkpoint: run the class again. Four tests pass.

Filter Real Data

Goal: use a query parameter on a real endpoint, not only the echo.

1
2
3
4
5
6
7
8
9
10
@Test
void queryParametersFilterRealData() {
    paymentsApi()
        .queryParam("currency", "SEK")
    .when()
        .get("/payments")
    .then()
        .statusCode(200)
        .body("count", equalTo(0));
}

What’s happening:

  • No test in this module creates a payment in Swedish krona, so count is 0 whatever other tests created, and the test doesn’t depend on run order.

Checkpoint: run the class again. Five tests pass.

Send Flags and Dates as Query Parameters

Send a Parameter Without a Value

Goal: send a flag: a name with no value.

1
2
3
4
5
6
7
8
9
@Test
void aQueryParameterWithoutAValue() {
    paymentsApi()
        .queryParam("verbose")
    .when()
        .get("/echo")
    .then()
        .body("rawQuery", equalTo("verbose"));
}

What’s happening:

  • queryParam("verbose") with no value sends only the name, without an =.
  • The server decodes it as verbose with one empty value, [""].

Checkpoint: run the class again. Six tests pass.

Send a Date

Goal: pass a java.time value without formatting it first.

1
2
3
4
5
6
7
8
9
@Test
void javaTimeValuesAreSentAsIso8601() {
    paymentsApi()
        .queryParam("from", LocalDate.of(2026, 1, 31))
    .when()
        .get("/echo")
    .then()
        .body("query.from", contains("2026-01-31"));
}

What’s happening:

  • Since REST Assured 6.1, a java.time value is sent with its toString(), which is ISO 8601.
  • The same works for an Instant or an OffsetDateTime.

Checkpoint: run the class again. Seven tests pass.

Control URL Encoding

Encode a Value With an Ampersand

Goal: prove that a & in a value doesn’t split the parameter.

1
2
3
4
5
6
7
8
9
@Test
void valuesAreUrlEncodedByDefault() {
    paymentsApi()
        .queryParam("name", "Anna de Vries & Co")
    .when()
        .get("/echo")
    .then()
        .body("query.name", contains("Anna de Vries & Co"));
}

What’s happening:

  • Without encoding, the & would end the parameter and start a new one called ` Co`.
  • REST Assured encodes the spaces and the &, so the server decodes the value back in one piece.

Checkpoint: run the class again. Eight tests pass.

See an Encoded Value Encoded Again

Goal: show what happens to a value you encoded yourself.

1
2
3
4
5
6
7
8
9
@Test
void anAlreadyEncodedValueIsEncodedAgain() {
    paymentsApi()
        .queryParam("q", "a%20b")
    .when()
        .get("/echo")
    .then()
        .body("rawQuery", equalTo("q=a%2520b"));
}

What’s happening:

  • REST Assured can’t tell an encoded value from one that happens to contain %, so it encodes the % as %25.
  • The server then decodes a%2520b to a%20b, not to a b.

Checkpoint: run the class again. Nine tests pass.

Turn Encoding Off

Goal: send a value that’s already encoded, as it is.

When you already have encoded values, such as from a copied URL, turn encoding off:

1
2
3
4
5
6
7
8
9
10
11
@Test
void urlEncodingCanBeTurnedOff() {
    paymentsApi()
        .urlEncodingEnabled(false)
        .queryParam("q", "a%20b")
    .when()
        .get("/echo")
    .then()
        .body("rawQuery", equalTo("q=a%20b"))
        .body("query.q", contains("a b"));
}

What’s happening:

  • urlEncodingEnabled(false) sends every value as it is.
  • rawQuery shows a%20b went on the wire unchanged, and query.q shows the server decoded it to a b.

Checkpoint: run the class again. Ten tests pass.

urlEncodingEnabled(false) applies to the whole request, including its path parameters. From then on, every value must already be encoded, or a space or & breaks the URL.

Send a Form Body

Send Form Parameters to the Echo

Goal: send two form fields and see that they travel in the body, not the URL.

1
2
3
4
5
6
7
8
9
10
11
12
@Test
void formParametersBecomeAUrlEncodedBody() {
    paymentsApi()
        .formParam("name", "Jan Jansen")
        .formParam("iban", "NL69INGB0123456789")
    .when()
        .post("/echo")
    .then()
        .body("contentType", startsWith("application/x-www-form-urlencoded"))
        .body("rawQuery", equalTo(""))
        .body("body", equalTo("name=Jan%20Jansen&iban=NL69INGB0123456789"));
}

What’s happening:

  • REST Assured sets the content type for you. It adds a charset, ; charset=ISO-8859-1, so the test uses startsWith(..).
  • rawQuery is empty: nothing is in the URL.
  • The space is sent as %20, not +. Browsers send + in forms, but some servers decode + wrongly, while every server reads %20 correctly.

Checkpoint: run the class again. Eleven tests pass.

Create a Beneficiary With a Form

Goal: send a form to a real endpoint and check what it created.

POST /beneficiaries only accepts a form, like an HTML page would send:

1
2
3
4
5
6
7
8
9
10
11
12
@Test
void aFormCreatesABeneficiary() {
    paymentsApi()
        .formParam("name", "Jan & Piet")
        .formParam("iban", "NL69INGB0123456789")
    .when()
        .post("/beneficiaries")
    .then()
        .statusCode(201)
        .body("name", equalTo("Jan & Piet"))
        .body("$", not(hasKey("nickname")));
}

What’s happening:

  • "Jan & Piet" survives the trip because the & is encoded.
  • "$" is the GPath for the whole body.
  • The API leaves out fields that have no value, so a beneficiary without a nickname has no nickname key at all. not(hasKey("nickname")) checks exactly that.

Checkpoint: run the class again. Twelve tests pass.

A form is the body, so you can’t send both. formParam(..) together with body(..) makes REST Assured throw IllegalStateException: You can either send form parameters OR body content in POST, not both!

Know Where param(..) Puts Its Value

Send param(..) With GET

Goal: show that param(..) goes into the query string for a GET.

1
2
3
4
5
6
7
8
9
10
@Test
void paramGoesIntoTheQueryForGet() {
    paymentsApi()
        .param("status", "RECEIVED")
    .when()
        .get("/echo")
    .then()
        .body("rawQuery", equalTo("status=RECEIVED"))
        .body("bodyLength", equalTo(0));
}

What’s happening:

  • With GET, param(..) behaves like queryParam(..), and bodyLength is 0.

Checkpoint: run the class again. Thirteen tests pass.

Send param(..) With POST

Goal: show that the same call puts the value in a form body for a POST.

The only change from the previous test is post(..) instead of get(..):

1
2
3
4
5
6
7
8
9
10
@Test
void paramGoesIntoAFormBodyForPost() {
    paymentsApi()
        .param("status", "RECEIVED")
    .when()
        .post("/echo")
    .then()
        .body("rawQuery", equalTo(""))
        .body("body", equalTo("status=RECEIVED"));
}

What’s happening:

  • With POST and no other body, param(..) behaves like formParam(..).
  • rawQuery is empty, so a server that reads status from the query string would never see it. That’s the silent filter from the opening.

Checkpoint: run the class again. All 14 tests pass.

When a value must be in the URL whatever the method, use queryParam(..). A POST can carry both: queryParam("a", "1") with formParam("b", "2") sends a=1 in the query and b=2 in the body.

Run the Tests

Run the following command from the repository root:

1
mvn test -pl module-02 -Dtest=ParametersTest

You should see a result similar to the following:

1
2
3
4
5
6
[INFO] Running com.javanibble.module_02.section_02.ParametersTest
[INFO] Tests run: 14, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.503 s -- in com.javanibble.module_02.section_02.ParametersTest
[INFO]
[INFO] Tests run: 14, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] BUILD SUCCESS

Make a Test Fail

In oneQueryParameter, change contains("RECEIVED") to equalTo("RECEIVED") and run the command again. You should see a result similar to the following:

1
2
3
4
5
[ERROR] Failures:
[ERROR]   ParametersTest.oneQueryParameter:59 1 expectation failed.
JSON path query.status doesn't match.
Expected: RECEIVED
  Actual: <[RECEIVED]>

The brackets in <[RECEIVED]> show the actual value is a one-element list, while the expected value is a string. When Expected and Actual look almost the same, look for brackets as well as types.

Change equalTo("RECEIVED") back to contains("RECEIVED") and run the tests once more to confirm they pass.

Troubleshooting

SymptomLikely causeFix
Expected: RECEIVED but Actual: <[RECEIVED]>The echo’s query values are listsUse contains(..) or hasItem(..)
rawQuery shows %2520 instead of %20The value was encoded before it was passed inPass the plain value, or use urlEncodingEnabled(false)
IllegalStateException: You can either send form parameters OR body content in POST, not both!The request has formParam(..) and body(..)Send the data as a form or as a body, not both
422 from POST /beneficiaries with "field": "iban"The form is missing a valid iban fieldSend both name and iban

Try It Yourself

Exercise 1: Send Two Currencies and a Start Date

Add a test called twoCurrenciesAndAStartDate that sends the currencies EUR and GBP under one name, currency, and the date 1 March 2026 as from. Check the whole rawQuery, and check the decoded currencies in order.

Hint

Call queryParam(..) twice: once with two values, once with a LocalDate. Parameters are sent in the order you add them.

Solution
1
2
3
4
5
6
7
8
9
10
11
@Test
void twoCurrenciesAndAStartDate() {
    paymentsApi()
        .queryParam("currency", "EUR", "GBP")
        .queryParam("from", LocalDate.of(2026, 3, 1))
    .when()
        .get("/echo")
    .then()
        .body("rawQuery", equalTo("currency=EUR&currency=GBP&from=2026-03-01"))
        .body("query.currency", contains("EUR", "GBP"));
}

Exercise 2: Create a Beneficiary With a Nickname

Add a test called aBeneficiaryCanHaveANickname. Send a form with the name "Anna de Vries", the IBAN "NL91ABNA0417164300" and the nickname "Mum" to POST /beneficiaries. Check the status code, that the Location header starts with /api/v1/beneficiaries/ben-, and that the body has the right name and nickname.

Hint

Start from aFormCreatesABeneficiary and add a third formParam(..). Check the header with header(..) and startsWith(..), as in Check Every Part of an HTTP Response.

Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Test
void aBeneficiaryCanHaveANickname() {
    paymentsApi()
        .formParam("name", "Anna de Vries")
        .formParam("iban", "NL91ABNA0417164300")
        .formParam("nickname", "Mum")
    .when()
        .post("/beneficiaries")
    .then()
        .statusCode(201)
        .header("Location", startsWith("/api/v1/beneficiaries/ben-"))
        .body("name", equalTo("Anna de Vries"))
        .body("nickname", equalTo("Mum"));
}

Summary

You built ParametersTest, which sends query parameters, flags, dates, encoded and unencoded values, form bodies and param(..), and proves on the wire where each one went. A filter that silently moved into the body now turns a test red.

  • queryParam(..) always writes to the URL, formParam(..) always writes a form body, and param(..) picks from the HTTP method.
  • A parameter with several values repeats its name, and a parameter without a value sends only its name.
  • java.time values are sent as ISO 8601 strings, so pass them as they are.
  • REST Assured encodes every value, including the % of a value you encoded yourself, unless you call urlEncodingEnabled(false).
  • In the echo, query values are lists, so check them with contains(..), hasItem(..) or everyItem(..).

Here are the commands you used in this guide:

1
mvn test -pl module-02 -Dtest=ParametersTest

What’s Next

Next: continue with Send Headers and Cookies in REST Assured, which explains the charset you saw on the form’s content type, and shows how to send headers and cookies.

The code:

Go deeper:

Related guides:

This post is licensed under CC BY 4.0 by the author.