Pauls
Pauls

Reputation: 3

Scala - Finatra - Reading ip address from header

I'm new to Finatra and scala. I need to store the ip address with another data in a post request. I'm using a custom case class: case class MyRequest(name : String, email: String) How can I also get remoteAddress in this request? Thanks in advance.

Upvotes: 0

Views: 467

Answers (2)

kemiya
kemiya

Reputation: 470

I just find it in the document, you can implement your case class looks like this:

case class MyRequest (
  request: Request,
  name : String,
  email: String
)

and the request is the type of com.twitter.finagle.http.Request, so you can access remoteAddress in this way:

post("/[your-api]") { r: MyRequest
  val ip = r.request.remoteAddress
}

Upvotes: 2

Jiji TANG
Jiji TANG

Reputation: 91

It seems you can't. If your request type is a custom case class. Probably you can do something like the following, and use jackson for deserialisation the request to your own case class:

  post("/{your-api-path}/") {
    r: Request =>

      val remoteIP = r.remoteHost
      val myRequest = objectMapper.readValue(r.contentString, classOf[MyRequest])
      ...
  }

Upvotes: 0

Related Questions