Nagireddy Hanisha
Nagireddy Hanisha

Reputation: 1440

What is an entity in Akka-Http?

I am new to akka-http and building a basic server-client application in scala. The examples I looked at has the object "entity". Can someone please explain the concept underlying and why is it used and how is it useful?

post {
    path("insert") {
      entity(as[Student]) {
        obj => complete {
          insertingstudent(obj)
          s"got obj with name ${obj.getName()}"
        }
      }

Thanks

Upvotes: 4

Views: 1149

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149548

Can someone please explain the concept underlying and why is it used and how is it useful?

entity is of type HttpEntity. From the comments of the code:

Models the entity (aka "body" or "content") of an HTTP message.

It is an abstraction over the content of the HTTP request. Many times, when one sends an HTTP request, they provide a payload inside the body of the request. This body can be in many format, popular ones are JSON and XML.

When you write:

entity(as[Student])

You are attempting to unmarhsall, or deserialize, the body of the request into a data structure of your liking. That means that your obj field in the proceeding function will be of type Student.

Upvotes: 6

Related Questions