reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

Reading the body of a request

Open rogusdev opened this issue 2 years ago • 2 comments

I want to be able to read the body of a (multipart) request I have built, for tests, but I cannot because there does not seem to be a public API for converting a Body (async nor blocking) into bytes. It has an as_bytes() method, but for Inner::Streaming it returns None (same for blocking's Kind::Reader) so there seems to be no way to publicly access that data when it needs to be streamed to turn into bytes. Am I missing something to do this? Or is this a missing feature?

To be clear, I see the tests: https://github.com/seanmonstar/reqwest/blob/master/tests/multipart.rs that compare against what was received by an actual http server. And the unit tests access private methods.

Thanks.

rogusdev avatar Dec 30 '23 01:12 rogusdev

Am I missing something to do this? Or is this a missing feature?

You're not missing anything. It so far was not publicly exposed. In #2059, there's a possibility of keeping the public impl HttpBody for Body, in which case you could use that.

the unit tests access private methods.

The integration tests (in the tests directory) all use public methods, they are compiled as separate from the library, they don't have any internal access.

seanmonstar avatar Jan 01 '24 17:01 seanmonstar

possibility of keeping the public impl HttpBody for Body,

Sounds good, I would appreciate that, thanks :)

The integration tests (in the tests directory) all use public methods, they are compiled as separate from the library, they don't have any internal access.

Yes, that's why I mentioned the unit tests as separate, e.g. https://github.com/seanmonstar/reqwest/blob/4f54ba732f80ccb89e50954a369d6e8bb46375f2/src/async_impl/multipart.rs#L591 which does exactly what I want -- but relies on private methods.

rogusdev avatar Jan 01 '24 19:01 rogusdev