Again, not necessarily the best solution but ..
Note: Some of the code here is in Java, so you will need to do some
digging/translation
Modifying the akka.http.marshallers.jackson.Jackson (Scala)
(possibly creating your own copy with the modifications)
Adding the following
def jsonAsString(): Unmarshaller[String] =
UnmarshallerImpl[String] { (_ec, _flowMaterializer) ⇒
implicit val ec = _ec
implicit val mat = _flowMaterializer
unmarshalling.Unmarshaller.messageUnmarshallerFromEntityUnmarshaller { //
isn't implicitly inferred for unknown reasons
unmarshalling.Unmarshaller.stringUnmarshaller
.forContentTypes(`application/json`)
}
}(ClassTag(classOf[String]))
You can then use the RequestVal (java)
RequestVal<String> jsonStringExtractor =
RequestVals.entityAs(MyJackson.jsonAsString());
To extract the JSON String
@Override
public Route createRoute() {
return route(
pathPrefix("jsontest").route(
post(handleWith(stringParser, (ctx, theRequest) -> {
System.out.println("JSON String :: "+theRequest);
return ctx.complete(createHttpResponse("OK"));
})))
);
Hope this helps, feedback is more than welcome
--
Mohamed
On Friday, January 16, 2015 at 3:26:48 PM UTC+2, Robin Bakkerus wrote:
>
> Can some one help me with (I think) a very simply question, but one I
> could not figure out myself yet.
>
> It is about how extract the (json) string out of the RequestEntity, inside
> a HttpRequest
>
> From the command line I give a command like this:
> curl -i -H "Accept: application/json" -H "Content-Type:application/json"
> -X POST --data '{"value":{"id":1}' http://localhost:8000/saveTsta
>
> And this should be handled by an Akka-http server like this:
>
> def requestHandler(req: HttpRequest): Future[HttpResponse] = req match {
> case HttpRequest(POST, Uri.Path("/saveTsta"), _, _, _) =>
> saveTsta(req)
> case _: HttpRequest =>
> unknownReq(req)
> }
>
>
> def saveTsta(req: HttpRequest): Future[HttpResponse] = {
> val entity:RequestEntity = req.entity
>
> val json:String = how_do_get_the_json_data_out_of(entity) ???
> }
>
> Txs in advance
>
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.