codingsplash
codingsplash

Reputation: 5045

How to send a response code as a response in Akka Http?

I have the below code:

val route:Route={
    path("hello"){
      get{
        complete{
          "done"
        }
      }
    }
  }

  Http().bindAndHandle(route, "localhost", 8187)

Here the complete responds back with string "done". However, I want it to return a status code of 200. How can I do this?

Upvotes: 3

Views: 4459

Answers (2)

gaston
gaston

Reputation: 498

You can use HttpResponse class

 import akka.http.scaladsl.model._

 complete{
    HttpResponse(StatusCodes.OK, entity = "Result ok")
   //HttpResponse(StatusCodes.InternalServerError, entity = "Error") 
  }

Upvotes: 4

As explained in the comments the default response code is 200, so you're getting exactly what you wanted. In general, the documentation on complete demonstrates how to write a complete with any status code:

complete(StatusCodes.OK)

complete(StatusCodes.Create -> "message")

complete(201 -> "another message")

Upvotes: 10

Related Questions