How to pass the post request body?

def girserviceRoute: Route = path(“girservice” / “globalitemrules” / “locationrules” / “v1” / “us” / “Walmart” ) {
post {
extractRequest { req =>
log.debug(s"received sbu request, ${req.method} ${req.uri} ${req.headers} ${req.entity}“)
req.headers.map(x => log.debug(x.name()))
val hdrs = req.headers.filter(header => (header.isNot(“timeout-access”) && header.isNot(“host”)))
val bodyDataReq: RequestEntity = req.entity
print(“body Data Request”, bodyDataReq)
hdrs.map(x => log.debug(x.name()))
val girServiceUri = s”${girServiceHost}/girservice/globalitemrules/locationrules/v1/us/Walmart"
val responseFuture = http
.singleRequest(request = HttpRequest(uri = Uri(girServiceUri).withRawQueryString(req.uri.rawQueryString.getOrElse(“”)), headers = hdrs , entity = req.entity ), connectionContext = httpsCtx)
.flatMap(response => response.entity.dataBytes.runReduce(_ ++ _))
onComplete(responseFuture) {
case Success(resp) => complete {
val st = resp.decodeString(StandardCharsets.UTF_8).replace(“5585”, “”)
log.debug(“response is " + st)
HttpEntity(ContentTypes.application/json, st)
}
case Failure(ex) => complete {
ex.printStackTrace()
log.error(ex, ex.getMessage)
(StatusCodes.InternalServerError, s"An error occurred: ${ex.getMessage}”)
}
}
}
}
}

In the above code, how can i capture the request body?

val bodyDataReq: RequestEntity = req.entity
print(“body Data Request”, bodyDataReq)

is not working.

No compile time error. but getting run time result as below.

(body Data Request,HttpEntity.Strict(application/json,0 bytes total))

Please use Markdown so that your code is properly indented. Unless you do that, your code is very hard to read, and that makes it hard to help you.

Do any of the methods on https://doc.akka.io/api/akka-http/current/akka/http/scaladsl/model/RequestEntity.html seem relevant?

HI I solved that issue. But I am seeing different issue. When I execute the code. I am seeing the runtime error as “

HTTP Status 415 - Unsupported Media Type

” Could you please help here.
def girserviceRoute: Route = path("girservice" / "globalitemrules" / "locationrules" / "v1" / "us" / "Walmart") {
    post {
      entity(as[String]) { reqData =>
        extractRequest { req =>
          log.debug(s"received sbu request, ${req.method} ${req.uri}   ${req.headers} ${req.entity}")
          req.headers.map(x => log.debug(x.name()))
          val hdrs = req.headers.filter(header => (header.isNot("timeout-access") && header.isNot("host")))
          hdrs.map(x => log.debug(x.name()))
          val girServiceUri = s"${girServiceHost}/girservice/globalitemrules/locationrules/v1/us/Walmart"
          val responseFuture = http
            .singleRequest(
              request = HttpRequest(method = HttpMethods.POST, uri = Uri(girServiceUri).withRawQueryString(req.uri.rawQueryString.getOrElse("")),
                headers = hdrs,
                entity = HttpEntity(reqData)),
              connectionContext = httpsCtx)
            .flatMap(response => response.entity.dataBytes.runReduce(_ ++ _))
          onComplete(responseFuture) {
            case Success(resp) => complete {
              val st = resp.decodeString(StandardCharsets.UTF_8).replace("5585", "")
              log.debug("response is " + st)
              HttpEntity(ContentTypes.`application/json`, st)
            }
            case Failure(ex) => complete {
              ex.printStackTrace()
              log.error(ex, ex.getMessage)
              (StatusCodes.InternalServerError, s"An error occurred: ${ex.getMessage}")
            }
          }
        }
      }
    }
  }

What do you know about 415 error codes and why they might occur? What have you done so far to try to troubleshoot the problem?

looks like the action has moved to Unsupported Media type