Akka stream for returning single json objects

Hello there,
I use akka http and the stream API and i would like to return only one element as a json object to the client but the server wrap the object inside an array.

        "items": []

With this code:

path( "service") {
      post {
        entity(as[FeedRequest]) { fr =>

Do you have a solution to return a single value?

I use akka stream for the back pressure because my service depends on some other http services that might be slow. Is akka stream a good choice to solve this problem?

Thank you

Finally I could return a single json by adding an implicit in scope (deduced from the documentation: link):

implicit val jsonStreamingSupport = EntityStreamingSupport.json(1)

However, i would still appreciate if someone confirm that it is a proper usage of the akka stream api.


Edit: I was expecting the stream to work accross all the incoming requests, and that the akka server would be able to drop the incoming request when it notice that an upstream service is slow. What I see is backpressure within one single request and for returning a stream of objects.

Hi @owjm,

each request arriving at your route will create a dedicated stream so two concurrent requests will not interact with each other. This means that if you want your code to backpressure when you detect the downstream HTTP services are slowing down, then you need a slightly different approach.

You should create a Flow[Req,Resp,...] using Akka HTTP’s Flow-based client (also known as superPool). This HTTP client would be part of an application-wide Akka Streams using a Source.queue source where each of your routes can offer elements to. The Flow provided by Akka HTTP’s supoerPool would then emit HttpResponse's that you would need to correlate with the input requests to complete each incoming request you had.

With this approach you would be able to backpressure from your service when the downstream HTTP services are overwelmed.


1 Like

Thanks @ignasi35 , that’s very insightful! I’ll try that