skip to content

In REST Assured, what does given().request(Method, path) do that get() and post() cannot?

level: middleimportance: nice to knowfreq 29%

answer

  1. the verb becomes an argument
  2. eight constants in the Method enum
  3. String overload sends any verb
  4. path resolution is unchanged
  5. URI and URL forms bypass the defaults

basics

~20 s

REST Assured's request method takes the HTTP verb as a value rather than as a method name: the eight Method enum constants, or any verb at all through the String overload. Path resolution against baseUri and basePath is unchanged.

solid answer

~50 s

`request(...)` is the generic sender the named verbs are built on, and it accepts the verb as an argument. `request(Method.GET, "/plots")` lets a parameterised test drive several verbs from one chain, and `request("REPORT", "/plots")` sends a verb that has no constant at all — `io.restassured.http.Method` declares exactly eight: `GET`, `PUT`, `POST`, `DELETE`, `HEAD`, `TRACE`, `OPTIONS`, `PATCH`. Targeting is identical to the named verbs: the `String path` overloads resolve against the base URI and base path with slashes normalised, while the `URI` and `URL` overloads are absolute and therefore bypass both. The no-path forms send an empty path, so `given().basePath("/api/v2/plots").request(Method.GET)` hits the base path itself. Reach for it when the verb is data — one loop over several verbs asserting `405` on a read-only resource — or when a shared helper takes the verb as a parameter. For an ordinary hand-written test, `get("/plots")` still reads better.

code

java · 22 lines
java
import io.restassured.http.Method;
import static io.restassured.RestAssured.given;

// The verb is a value: one chain, driven from data
for (Method method : new Method[]{Method.PUT, Method.DELETE, Method.PATCH}) {
    given()
        .baseUri("https://rota.allotment-water.test")
        .basePath("/api/v2")
    .when()
        .request(method, "/plots/17/slots")
    .then()
        .statusCode(405);
}

// A verb with no Method constant
given()
    .baseUri("https://rota.allotment-water.test")
    .basePath("/api/v2")
.when()
    .request("REPORT", "/plots")
.then()
    .statusCode(501);

go deeper

for a junior

Know that request(...) exists and that the named verbs are shorthand for it. Being able to say the verb is passed as an argument, and that targeting works the same way, is enough here.

for a middle

Explain the overload set and the eight Method constants, and be precise that path resolution is identical to get(...) while the URI and URL forms bypass the base URI and base path.

for a senior

Show where it pays off in a real suite — data-driven verb coverage, a shared send helper, a verb outside the enum — and why an ordinary test should still use the named verb for readability.

for a principal

Weigh the readability cost of routing every call through a generic sender against the leverage of one place owning logging, auth and target defaults, and say where you would draw that line for a team.

`get(...)`, `post(...)`, `put(...)` and their siblings are convenience methods on top of a single generic sender. That sender is `request(...)`, and it is what you reach for when the HTTP verb has to be a **value** rather than a method name. ## Two things the named verbs cannot do - **Take the verb as data.** `request(Method.GET, "/plots")` and `request(Method.PATCH, "/plots/17")` differ only in an argument, so a parameterised test can drive the same chain across several verbs from a data source. - **Send a verb with no constant.** The `String` overloads — `request("REPORT", "/plots")` — send whatever verb you name. The `Method` enum's own documentation says as much: any verb is supported when you go through `request`. ## The eight Method constants `io.restassured.http.Method` declares exactly eight values, and no more: - `GET`, `PUT`, `POST`, `DELETE` - `HEAD`, `TRACE`, `OPTIONS`, `PATCH` Each has a matching named verb method on the DSL, so `request(Method.HEAD, "/plots")` and `head("/plots")` are equivalent. If you need something outside this set — a WebDAV verb, or a bespoke one a service defines — the enum is a dead end and the `String` overload is the answer. It is a plain Java enum, so it cannot be extended at runtime and there is no registration hook to add a ninth constant. That is not a limitation in practice, because the `String` forms exist precisely to cover everything the enum does not. ## The overload set `request` comes in eight forms, four for the enum and four mirroring them for a raw `String`: | Form | What the target is | |---|---| | `request(Method)` / `request(String)` | base URI plus base path, with an empty path | | `request(Method, String path, Object... pathParams)` and its `String` twin | the usual assembly: base URI, base path, then this path | | `request(Method, URI)` / `request(String, URI)` | the URI itself — fully qualified, so the defaults are bypassed | | `request(Method, URL)` / `request(String, URL)` | the URL itself — same bypass | ## Where the call lands This is the part worth being precise about, because `request(...)` is not a different addressing mechanism: - The `String path` overloads resolve **exactly** as `get(...)` does — base URI, then base path, then the path — with slashes normalised at each seam. - The no-path forms pass an empty path internally, so `given().baseUri("https://rota.allotment-water.test").basePath("/api/v2/plots").request(Method.GET)` sends a GET to `https://rota.allotment-water.test/api/v2/plots`. That is a genuinely useful shape when the base path already names the resource. - The `URI` and `URL` overloads are fully qualified by construction, so they discard the base URI and base path the same way an absolute string in `get(...)` does. - Everything else on the chain — the port rule, headers, auth, filters — behaves identically. Only the way the verb is named has changed. ## When to reach for it - **Data-driven verb coverage.** Asserting that the allotment water-rota API answers `405` for every verb it does not implement on `/plots/17/slots` is one loop over `Method.values()` and one `request(method, path)` call, instead of eight near-identical tests. - **A verb the enum does not carry.** Use the `String` form and name the verb directly. - **A helper that wraps sending.** A shared method taking `(Method method, String path)` keeps one place responsible for logging, auth and base defaults. ## The cost side Routing everything through `request(...)` has a readability price that is easy to underestimate. A file of named verbs can be skimmed for what it does — the `post` calls are the writes, the `get` calls are the reads — and that scanning disappears the moment the verb becomes an argument. A generic sender also pushes the verb one level away from the assertion it belongs with, so a reviewer has to follow a parameter to know what was tested. So for an ordinary hand-written test, prefer the named verb: `get("/plots/17/slots")` reads better than `request(Method.GET, "/plots/17/slots")`. `request(...)` earns its place in exactly two situations — when the verb stops being a constant in the source and becomes a parameter, and when the verb has no constant to be. Those are real cases, and it is worth knowing the method is there for them, but they are the minority of calls in most suites.

  • Where does given().basePath("/api/v2/plots").request(Method.GET) send its request?
    To the base URI plus the base path, with an empty path. The no-path overloads pass an empty string internally, so with a base URI of `https://rota.allotment-water.test` the call reaches `https://rota.allotment-water.test/api/v2/plots`. No extra segment and no trailing slash are added.
  • Do the request(Method, URI) and request(Method, URL) overloads honour the base URI?
    No. A `URI` or `URL` is fully qualified by construction, so REST Assured uses it as the complete target and discards both the base URI and the base path — the same rule as passing an absolute string to `get(...)`. An explicitly set port is still appended if that target carries none.

saying these in an interview costs you the question

  • Thinks request() resolves paths differently from get()
  • Believes the Method enum can be extended for a custom verb
  • Assumes request(Method.GET) with no path adds a /GET segment
  • Says request() is the only way to send PATCH
  • Thinks the URI and URL overloads still append the base path