NSMutableURLRequest and “request body stream exhausted” error

前端 未结 2 1969
渐次进展
渐次进展 2021-01-13 18:06

I have a problem with http PUT request and request body as stream from file.

No matter what the size of the file i get error \"NSURLErrorDomain -1021 request body st

2条回答
  •  情深已故
    2021-01-13 18:38

    From the looks of it, it seems that you're not setting @"Content-Length" in the header.

    The way I do it is like this:

    NSUInteger fileSize = [[[[NSFileManager defaultManager] attributesOfItemAtPath:_dataStreamLocation error:nil] objectForKey:NSFileSize] unsignedIntegerValue];
    [request setValue:[NSString stringWithFormat:@"%u", fileSize] forHTTPHeaderField:@"Content-Length"];
    

    Either way, I was doing batch uploading and occasionally got the body stream exhaustion error. From what I could tell, the issue was that I only had few free space on the device, and the temporary files would get deleted automatically before some uploads being finished (when receiving the error I tested to check if the file was still there, and it wasn't).

提交回复
热议问题