I have a problem with an HTTP PUT request and requesting the body as a stream from a file.
No matter what file size I get, the error "NSURLErrorDomain -1021 requests the body stream is exhausted"
I know that I can override this problem by implementing a method:
-(NSInputStream*)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request
but this approach is not very good, since it will download the entire file again, and 40 MB of the file will turn out to be 80 MB of data transfer.
if I take the same file as NSData and set up the request body, it works fine.
I tried sending an Async request and synchronizing the same result in both.
Here is my code, simple and similar to an example from Apple:
NSURL *url = [NSURL URLWithString:[self concatenatedURLWithPath:path]]; NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url]; [req setHTTPMethod:@"PUT"]; [req setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setTimeoutInterval:DEFAULT_TIMEOUT]; [req setValue:_contentType forHTTPHeaderField:@"Content-Type"]; NSInputStream *fileStream = [NSInputStream inputStreamWithFileAtPath:_dataStreamLocation]; [req setHTTPBodyStream:fileStream]; _connection = [[NSURLConnection connectionWithRequest:req delegate:self] retain];
Am I doing something wrong? Did I miss something?
Eitan source share