Content-Length maximum allowed length

Vincent picture Vincent · Apr 2, 2014 · Viewed 12.5k times · Source

I'm programming in ObjectiveC. I guess that all oC programmers are using the rather standard code for posting:

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

Currently I'm getting warning that NSUInteger should not be used as format argument. The warning suggest a type cast (unsigned long). And also replacing %d with %lu. That sounds logical.

In the HTTP definition, I can not find how long the Content-Length may actually be. Just the rather vague Octet. Is that 8 bits? I can not find the answer!

Answer

user3386109 picture user3386109 · Apr 2, 2014

There is no specific limit on the maximum value for the Content-Length.

https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13

14.13 Content-Length
Any Content-Length greater than or equal to zero is a valid value.

Also the Apple documentation for setValue:forHTTPHeaderField: in the NSMutableURLRequest class reference states

Additionally, if the length of your upload body data can be determined automatically (for example, if you provide the body content with an NSData object), then the value of Content-Length is set for you.