* 19756: Add extractData and extractRequestEntity directives. remove unnecessary import * #19756: add documentation to extractDataBytes and extractRequestEntity directives
This commit is contained in:
parent
735060da20
commit
7fdd5983a3
8 changed files with 147 additions and 1 deletions
|
|
@ -795,5 +795,36 @@ class BasicDirectivesExamplesSpec extends RoutingSpec {
|
|||
}
|
||||
//#
|
||||
}
|
||||
"extractRequestEntity-example" in {
|
||||
//#extractRequestEntity-example
|
||||
val route =
|
||||
extractRequestEntity { entity =>
|
||||
complete(s"Request entity content-type is ${entity.contentType}")
|
||||
}
|
||||
|
||||
// tests:
|
||||
val httpEntity = HttpEntity(ContentTypes.`text/plain(UTF-8)`, "req")
|
||||
Post("/abc", httpEntity) ~> route ~> check {
|
||||
responseAs[String] shouldEqual s"Request entity content-type is text/plain; charset=UTF-8"
|
||||
}
|
||||
//#
|
||||
}
|
||||
"extractDataBytes-example" in {
|
||||
//#extractDataBytes-example
|
||||
val route =
|
||||
extractDataBytes { data ⇒
|
||||
val sum = data.runFold(0) { (acc, i) ⇒ acc + i.utf8String.toInt }
|
||||
onSuccess(sum) { s ⇒
|
||||
complete(HttpResponse(entity = HttpEntity(s.toString)))
|
||||
}
|
||||
}
|
||||
|
||||
// tests:
|
||||
val dataBytes = Source.fromIterator(() ⇒ Iterator.range(1, 10).map(x ⇒ ByteString(x.toString)))
|
||||
Post("/abc", HttpEntity(ContentTypes.`text/plain(UTF-8)`, data = dataBytes)) ~> route ~> check {
|
||||
responseAs[String] shouldEqual "45"
|
||||
}
|
||||
//#
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ Directive Description
|
|||
via the ``Accept-Encoding`` header (from a user-defined set)
|
||||
:ref:`-entity-` Extracts the request entity unmarshalled to a given type
|
||||
:ref:`-extract-` Extracts a single value using a ``RequestContext ⇒ T`` function
|
||||
:ref:`-extractDataBytes-` Extracts the entities data bytes as a stream ``Source[ByteString, Any]``
|
||||
:ref:`-extractClientIP-` Extracts the client's IP from either the ``X-Forwarded-``,
|
||||
``Remote-Address`` or ``X-Real-IP`` header
|
||||
:ref:`-extractCredentials-` Extracts the potentially present ``HttpCredentials`` provided with the
|
||||
|
|
@ -58,6 +59,7 @@ Directive Description
|
|||
:ref:`-extractMethod-` Extracts the request method
|
||||
:ref:`-extractRequest-` Extracts the current ``HttpRequest`` instance
|
||||
:ref:`-extractRequestContext-` Extracts the ``RequestContext`` itself
|
||||
:ref:`-extractRequestEntity-` Extracts the ``RequestEntity`` from the ``RequestContext``
|
||||
:ref:`-extractScheme-` Extracts the URI scheme from the request
|
||||
:ref:`-extractSettings-` Extracts the ``RoutingSettings`` from the ``RequestContext``
|
||||
:ref:`-extractUnmatchedPath-` Extracts the yet unmatched path from the ``RequestContext``
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
.. _-extractDataBytes-:
|
||||
|
||||
extractDataBytes
|
||||
================
|
||||
|
||||
Signature
|
||||
---------
|
||||
|
||||
.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/BasicDirectives.scala
|
||||
:snippet: extractDataBytes
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
Extracts the entities data bytes as ``Source[ByteString, Any]`` from the :class:`RequestContext`.
|
||||
|
||||
The directive returns a stream containing the request data bytes.
|
||||
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala
|
||||
:snippet: extractDataBytes-example
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
.. _-extractRequestEntity-:
|
||||
|
||||
extractRequestEntity
|
||||
====================
|
||||
|
||||
Signature
|
||||
---------
|
||||
|
||||
.. includecode2:: /../../akka-http/src/main/scala/akka/http/scaladsl/server/directives/BasicDirectives.scala
|
||||
:snippet: extractRequestEntity
|
||||
|
||||
Description
|
||||
-----------
|
||||
|
||||
Extracts the ``RequestEntity`` from the :class:`RequestContext`.
|
||||
|
||||
The directive returns a ``RequestEntity`` without unmarshalling the request. To extract domain entity,
|
||||
:ref:`-entity-` should be used.
|
||||
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
.. includecode2:: ../../../../code/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala
|
||||
:snippet: extractRequestEntity-example
|
||||
|
|
@ -17,11 +17,13 @@ on two axes: a) provide a constant value or extract a value from the ``RequestCo
|
|||
a single value or a tuple of values.
|
||||
|
||||
* :ref:`-extract-`
|
||||
* :ref:`-extractDataBytes-`
|
||||
* :ref:`-extractExecutionContext-`
|
||||
* :ref:`-extractMaterializer-`
|
||||
* :ref:`-extractLog-`
|
||||
* :ref:`-extractRequest-`
|
||||
* :ref:`-extractRequestContext-`
|
||||
* :ref:`-extractRequestEntity-`
|
||||
* :ref:`-extractSettings-`
|
||||
* :ref:`-extractUnmatchedPath-`
|
||||
* :ref:`-extractUri-`
|
||||
|
|
@ -94,10 +96,12 @@ Alphabetically
|
|||
cancelRejections
|
||||
extract
|
||||
extractExecutionContext
|
||||
extractDataBytes
|
||||
extractMaterializer
|
||||
extractLog
|
||||
extractRequest
|
||||
extractRequestContext
|
||||
extractRequestEntity
|
||||
extractSettings
|
||||
extractUnmatchedPath
|
||||
extractUri
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue