Context
I want to send an HTTP request with multipart body data composed by two segments:
- A JSON string containing some metadata
- Some file binary data
What I already know
This can be done easily as described in this answer. But what happens in a situation where the file can be potentially large? With this new constraint, we naturally can't encapsulate the data in a NSData object, as it would take a lot of memory resources.
The first idea that anyone familiar with the NSURLRequest class would have is to use the HTTPBodyStream property instead of HTTPBody. But how is it possible to get a stream that would write both JSON and file data segments (as well as the boundaries and the other required HTTP stuff)?
The way out would be to subclass NSInputStream. In fact I've noticed that the AFNetworking framework uses this approach. But from I've heard, there are a lot of undocumented/weird methods to override from the NSStream class, as described here, which, in my humble opinion, sounds like a lot of (potentially dangerous) work needed to achieve something apparently simple.
Question
Is there a way to achieve the desired behaviour? Please consider the following constraints:
- No third party frameworks
- Using a clean method (not overriding undocumented methods, as they can change on future SDKs)