http error: "request body stream exhausted"
阿新 • • 發佈:2019-01-18
request body stream exhausted #1713 Closed prathapkumar opened this issue on Dec 26, 2013 · 6 comments Assignees No one assigned Labels None yet Projects None yet Milestone No milestone Notifications Subscribe You’re not receiving notifications from this thread. 6 participants @prathapkumar @msencenb @mattt @rcabamo @ghousesgb @BB9z @prathapkumar prathapkumar commented on Dec 26, 2013 Hi every one , i am trying to upload a video file using afnetworking 2.x it shows an error like "'request body stream exhausted'" after searching a lot in web i got answer like 'upload again when you got this error' after uploading the second time it works , But uploading second time is not good here is my code -(void)uploadVideo { NSURLCredential nsCredential = [NSURLCredential credentialWithUser:@"*********_" password:@"_*********************" persistence:NSURLCredentialPersistenceForSession]; // NSURLCredential *nsCredential = [NSURLCredential credentialWithUser:@"*********************" password:@"********************" persistence:NSURLCredentialPersistenceForSession]; //[client setDefaultCredential:nsCredential]; //[manager setCredential:nsCredential]; NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"*****", @"userid", nil]; [params setObject:@"************" forKey:@"accesstoken"]; NSString *url = [ NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Apple2" ofType:@".mp4"]]; NSData *videoData = [NSData dataWithContentsOfFile:url]; AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer]; NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://something.com/ios/upload/video" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFileData:videoData name:@"file" fileName:@"Apple2.mp4" mimeType:@"video/mp4"]; [formData throttleBandwidthWithPacketSize:kAFUploadStream3GSuggestedPacketSize delay:kAFUploadStream3GSuggestedDelay]; }]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager setCredential:nsCredential]; AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure %@", error.description); [self uploadVideo]; }]; [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite); }]; [operation start]; Can you fix this error Thank you Regards Prathap.M @prathapkumar prathapkumar commented on Dec 27, 2013 Is there any one ? "help to me" ........... @msencenb msencenb commented on Jan 1, 2014 I'm currently investigating similar behavior but only when I receive an authentication challenge. Here's a POST, with 'app.server' being a subclass of AFHTTPRequestOperationManager (2.0.1) CURAppDelegate *app = [[UIApplication sharedApplication] delegate]; NSString *stringURL = @"http://example.com/myurlisactuallyhere"; [app.server POST:stringURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData){ [formData appendPartWithFileData:self.mediaData name:@"image_submission" fileName:@"iOS_image" mimeType:@"image/jpeg"]; [formData throttleBandwidthWithPacketSize:kAFUploadStream3GSuggestedPacketSize delay:kAFUploadStream3GSuggestedDelay]; } success:^(AFHTTPRequestOperation *operation, id responseObject){ callback(); } failure:^(AFHTTPRequestOperation *operation, NSError *error){ errorCallback(@"Image failed to upload"); }]; I hit the authentication challenge, and then if I add this nsurlconnectiondatadelegate method into AFHttpRequestOperation it gets tripped - (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request { DebugLog(@"need new stream"); return nil; } Have to go for the new years... but I will keep digging and looking at older issues @mattt Contributor mattt commented on Jan 17, 2014 In its current implementation, AFStreamingMultipartFormData cannot be copied and re-opened in the way that needNewBodyStream: mandates. Retuning nil in this method triggers automatic buffer rewind behavior that only takes effect with non-stream request bodies, which makes it a strong default. Retrying the request is the recommended solution. A future version of AFNetworking may address this issue more directly. @mattt mattt closed this on Jan 17, 2014 @rcabamo rcabamo commented on Feb 24, 2014 I have the same problem over Wifi connection and I receive the same error again and again. Any idea? Thanks