Get original bytes from request

165 views Asked by At

How I get original bytes from request? Calling request.body.asBytes() I get this message:

asBytes() expected list of bytes, instead got List<_InternalLinkedHashMap<String, dynamic>>

I saw that HTTPRequestBody has the property retainOriginalBytes to use in this case, but where I set it?

Thanks!

2

There are 2 answers

0
leerob On

Whatever endpoint you're hitting with your request is returning a Map in its body, not a list of Bytes.

I'm not sure if you can control the contents of what that endpoint is returning but if you can that would be the place to change it.

Check out the BytesBuilder class. Also, read the Aqueduct docs for Request and Response Objects. Hopefully this gets you on the right path!

0
Joe Conway On

You're on the right track; this will work correctly once you've set retainOriginalBytes to true. This must be done before the body is decoded.

In an HTTPController, the request body is decoded before your method that handles the request is called. Just before the decoding, HTTPController calls its willDecodeRequestBody() method. This method does nothing by default, but you can override it to set retainOriginalBytes:

@override
void willDecodeRequestBody(HTTPRequestBody body) {
  body.retainOriginalBytes = true;
}

Here is an example of an application that does this.