Reputation: 327
I have a problem using setHTTPBodyStream instead of setHTTPBody with a NSMutableURLRequest.
I'm working on code to send large file to a server through http post. With the following portion code, everything works perfectly :
NSData * mydata = [NSData dataWithContentsOfFile:self.tmpFileLocationToUpload];
[request setHTTPBody:mydata];
If I change it to :
NSData * mydata = [NSData dataWithContentsOfFile:self.tmpFileLocationToUpload];
self.tmpInputStream = [NSInputStream inputStreamWithData:mydata];
[request setHTTPBodyStream: self.tmpInputStream];
Then I always end with a network error : Error - The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 303.)
The goal is at the end to create inputStrem directly from the file to be able to send large file without to load them in memory.
Did I miss something with setHTTPBodyStream use ?
Thanks for your help.
Regards. Sébastien.
Upvotes: 1
Views: 1142
Reputation: 438152
When you use setHTTPBody
, it automatically sets the Content-length
header of the request, but when you use setHTTPBodyStream
, it doesn't, but rather sets Transfer-encoding
of chunked
. A chunked
transfer encoding is used when the length of the stream cannot be determined in advance. (See Chunked transfer encoding.)
When using NSURLConnection
in conjunction with setHTTPBodyStream
, you can manually set Content-length
and it will prevent the Transfer-encoding
of chunked
(assuming you know the length in advance):
NSString *contentLength = [NSString stringWithFormat:@"%d", [mydata length]];
[request setValue:contentLength forHTTPHeaderField:@"Content-length"];
When using NSURLSession
, attempts to manually set Content-length
in conjunction with a chunked request may not prevent it from being a chunked
request.
Upvotes: 1
Reputation: 7558
Try setting the HTTP method (if you're not already). I was able to fix a "kCFErrorDomainCFNetwork error 303" error by adding:
[request setHTTPMethod:@"POST"];
Upvotes: 1