How to get the raw request body in Play?

旧城冷巷雨未停 提交于 2020-01-16 08:24:12

问题


My play application has an endpoint for receiving webhooks from Stripe.

In order to verify the webhooks, the request body needs to be compared against a signature and a signing key. This requires that I have access to the raw request body, as sent.

However, it seems that Play alters the request body, and I can't get access to the raw contents. This causes the computed signature to change, and the verification fails. More info: https://stackoverflow.com/a/43894244/49153

Here's my code:

@Singleton
class WebhookController @Inject()(cc : ControllerComponents,
                                  env: Env)
                                 (implicit ec: ExecutionContext)
  extends AbstractController(cc) {

  private val log = Logger("WebhookController")


  def index(): Action[AnyContent] = Action.async { implicit req =>

      val signature =
          req.headers.headers.find(_._1 == "Stripe-Signature").map(_._2).getOrElse("").trim

      if (verifySignature(req.body.toString, signature, env.webhookSecretKey))
        Future.successful(ok("ok"))
      else
          Future.successful(ok("Couldn't verify signature"))
  }


}

Here I'm trying to access the body using req.body.toString but it looks to be the deserialized json rather than the raw body.

Using req.body.asRaw returns a none.

Any ideas?


回答1:


Solved this by using Action.async(parse.raw) and then doing req.body.asBytes().map(_.utf8String).getOrElse("") to obtain the raw string of the body. Some more info: https://www.playframework.com/documentation/2.7.x/ScalaBodyParsers



来源:https://stackoverflow.com/questions/59332440/how-to-get-the-raw-request-body-in-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!