pyke369 / PKMultipartInputStream

an NSInputStream subclass suitable for building multipart/form-data http requests bodies in MacOSX/iOS applications.
http://github.com/pyke369/PKMultipartInputStream
MIT License
73 stars 19 forks source link

Need Base64 encoding for binary attachments #21

Open robertcopper opened 6 years ago

robertcopper commented 6 years ago

I need to upload audio, video, and images as part of a multipart/form-data POST. These are best encoded in Base64.

The principal advantage is a predictable length. The request header requires a length. Is this the length of the encoded or the decoded data?

UTF-8 adds escape sequences to the data stream, which makes the length depend on the actual data, and not the size of the file.

jznadams commented 6 years ago

I'm handling file uploads like this:

PKMultipartInputStream *body;
...
[body addPartWithName:name filename:originalFilename path:filepath contentType:contentType];
...
[theRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];
robertcopper commented 6 years ago

I suggest that won't work. The encoded length of the UTF-8 stream depends upon the actual contents of the file. Base64 is guaranteed to be 4/3 * (the original length rounded up to a multiple of three).

Unless the length field refers to the length of the decoded data.

robertcopper commented 6 years ago

Apparently, it refers to the "transfer-length of the message-body". RFC 2616 section 14.13

https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

It's also apparently optional.

robertcopper commented 6 years ago

@jznadams

I did set the request length by your method for my revised demo app, but had not in my application's code. Our server apparently requires the parameter even if it's inaccurate.