views:

162

answers:

1

I am posting a .zip file from the iPhone to a Rails server using an NSURLRequest. The problem is that the content-type of the zip file is lost in-transit. When I upload the same zip file from a web-browser to Rails, the content-type is preserved. This leads me to believe it's related to the way I'm sending it from the iPhone. Does anyone have any idea why this might happen? I've posted the iPhone code below.

NSString *filePath = [self filePathForExportedData]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kExportURLString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];

[theRequest setHTTPMethod:@"POST"];

[theRequest setValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BOUNDARY]

forHTTPHeaderField:@"Content-Type"]; NSMutableData *fileData = [NSMutableData dataWithContentsOfFile:filePath]; NSMutableData *postData = [NSMutableData dataWithCapacity:[fileData length] + 512]; [postData appendData: [[NSString stringWithFormat:@"--%@\r\n", BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]]; [postData appendData: [[NSString stringWithFormat: @"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", @"archive_file", @"export.zip"] dataUsingEncoding:NSUTF8StringEncoding]]; [postData appendData: [[NSString stringWithFormat:@"Content-Type: application/zip\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [postData appendData: fileData]; [postData appendData: [[NSString stringWithFormat:@"\r\n--%@--\r\n", BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]]; [theRequest setHTTPBody:postData]; [self makeRequest:theRequest]; # This will send the request

A: 

I was able to fix the problem by removing the second newline in the "Content-Disposition" header, like so:

[postData appendData: [[NSString stringWithFormat: @"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n\r\n", @"archive_file", @"export.zip"]
                                       dataUsingEncoding:NSUTF8StringEncoding]];

I'm guessing that the double newline signifies the end of the headers and the "Content-Type" header was being ignored.

wdlindmeier