overrideMethodWithParameter • Akka HTTP (original) (raw)

Signature

def overrideMethodWithParameter(paramName: String): Directive0

Description

Changes the request method to the value of the specified query parameter.

Changes the HTTP method of the request to the value of the specified query string parameter. If the query string parameter is not specified this directive has no effect.

If the query string is specified as something that is not an HTTP method, then this directive completes the request with a 501 Not Implemented response.

This directive is useful for:

Example

Scala

source`val route = overrideMethodWithParameter("method") { concat( get { complete("This looks like a GET request.") }, post { complete("This looks like a POST request.") } ) }

// tests: Get("/?method=POST") ~> route ~> check { responseAs[String] shouldEqual "This looks like a POST request." } Post("/?method=get") ~> route ~> check { responseAs[String] shouldEqual "This looks like a GET request." }

Get("/?method=hallo") ~> route ~> check { status shouldEqual StatusCodes.NotImplemented }`

Java

source`import static akka.http.javadsl.server.Directives.complete; import static akka.http.javadsl.server.Directives.get; import static akka.http.javadsl.server.Directives.post; import static akka.http.javadsl.server.Directives.overrideMethodWithParameter;

final Route route = concat( overrideMethodWithParameter("method", () -> concat( get(() -> complete("This looks like a GET request.")), post(() -> complete("This looks like a POST request.")) ) ) );

// tests: testRoute(route).run(HttpRequest.GET("/?method=POST")).assertEntity( "This looks like a POST request.");

testRoute(route).run(HttpRequest.POST("/?method=get")) .assertEntity("This looks like a GET request.");

testRoute(route).run(HttpRequest.GET("/?method=hallo")).assertEntity( "The server either does not recognize the request method, or it lacks the ability to fulfill the request."); `

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.