Hi,
I have the following bit of code:
- (void) uploadFileAtURL:(NSURL*)localURL toPath:(NSString*)inPath { NSURL * remoteURL = [ [NSURL URLWithString:inPath relativeToURL:self.baseURL] absoluteURL ] ; NSInputStream * inputStream = [NSInputStream inputStreamWithURL:localURL] ; NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:remoteURL ] ; [request setHTTPMethod:@"PUT"] ; [request setHTTPBodyStream:inputStream] ; NSLog ( @"Stream: %@" , inputStream ) ; NSLog ( @"Local URL: %@" , localURL ) ; NSLog ( @"toPath: %@" , inPath ) ; NSLog ( @"Remote URL: %@" , remoteURL ) ; NSLog ( @"Request: %@" , request ) ; NSHTTPURLResponse * response = nil ; NSError * error = nil ; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error] ; NSInteger _statusCode = [response statusCode] ; NSString* _statusString = [NSHTTPURLResponse localizedStringForStatusCode:_statusCode] ; NSLog ( @"*** Status: %i - %@" , _statusCode , _statusString ) ; NSLog ( @"*** Error: %@" , error ) ; }
This code, when executed, returns the following output to the console:
Stream: <__NSCFInputStream: 0x6324830> Local URL: file://localhost/Temporary/IMG_0013.PNG toPath: IMG_0013.PNG Remote URL: http://idisk.me.com/myUserName/IMG_0013.PNG> Request: <NSMutableURLRequest http://idisk.me.com/myUserName/IMG_0013.PNG>> *** Status: 0 - server error *** Error: Error Domain=NSURLErrorDomain Code=-1021 "request body stream exhausted" UserInfo=0x632b750 {NSErrorFailingURLStringKey=http://idisk.me.com/kemsleyc/IMG_0013.PNG>, NSErrorFailingURLKey=http://idisk.me.com/kemsleyc/IMG_0013.PNG>, NSLocalizedDescription=request body stream exhausted, NSUnderlyingError=0x632d600 "request body stream exhausted"}
I printed out the existence of the stream just to prove that it's there and not nil. The file *does* work when I use the stream for anything else - just not this.
I don't understand what it means by "request body stream exhausted" - duh? It hit the end of the file. It's supposed to handle that, right?
Any help would be greatly appreciated
When doing a multipart POST, if there's an authentication challenge, I'm getting a 'stream exhausted' error.css
After a bit of research, I found that after a challenge, the request will be retried, which means that the NSInputStream needs to be read from the beginning again. The documentation on this is confusing. Comments in NSURLConnection.h imply that the data will be spooled to disk so the connection can be restarted, but the web docs say spooling only happens on OSX:html
https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSURLConnectionDelegate/connection:needNewBodyStreamnode
To fix this on iOS, AFNetworking should implement needNewBodyStream. There's a few posts on devforums.apple.com that recommend doing this:react
https://devforums.apple.com/message/237388#237388
https://devforums.apple.com/message/344093#344093ios