1

I'm trying to get the raw request body in a Micronaut controller. The endpoint must consume application/json, but we need the raw json to generate a signature that must match a signature found in the header. No matter what @Body type I set or even if I grab the HttpRequest.body, it has been modified. Unfortunately, I'm implementing a third-party API, else I would change the content-type to text/plain and all would work just fine.

@Consumes(MediaType.APPLICATION_JSON)
 @Post("/controlMessages")
 open fun recvControl2(@Header("signature")  signature: String, request: HttpRequest<Any>, @Body payload: Any): HttpResponse<Any> {
     LOG.info("body=${request.body.get()}")

     when(isBodyValid(signature, request.body.get())){
         true -> return HttpResponse.ok()
         false -> return HttpResponse.unauthorized()
     }
 }

Notice that the DefaultHttpClient shows the unmodified body


    Sending HTTP Request: POST /controlMessages
    DefaultHttpClient - Chosen Server: localhost(27813)
    DefaultHttpClient - signature: 8e85acdb42cb5e14e15f1fbd954a8e98f1912fa1283bace5913
    DefaultHttpClient - host: localhost:27813
    DefaultHttpClient - connection: close
    DefaultHttpClient - content-type: application/json
    DefaultHttpClient - content-length: 103
    DefaultHttpClient - Request Body
    DefaultHttpClient - ----
    DefaultHttpClient - {"cancelControlMessages": [
     {"remoteId": "some-remote-id", "sentAt": "2019-11-06T14:55:15.603698Z"}
    ]}
    DefaultHttpClient?[0;39m - ----
    ControlController?[0;39m - body={"cancelControlMessages":[{"remoteId":"some-remote-id","sentAt":"2019-11-06T14:55:15.603698Z"}]}

Scott
  • 136
  • 2
  • 6

1 Answers1

2

This was added in 1.3.0.M1. You can use @Body payload: String to receive the raw body

James Kleeh
  • 12,094
  • 5
  • 34
  • 61