0

I've searched for this issue and found similar problems but none like it.

I'm building a request manually

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:TEST_URL]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=upload[jsondata]\r\n\n Content-Type: application/octet-stream\r\n\r\n", [dateFormatter stringFromDate:startDate]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[json dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

When this request reaches the rails server, it understands Content-Disposition, Content-Type, but the JSON part of the body is captured as "ActionDispatch::Http::UploadedFile", meaning the server isn't understanding the JSON and is looking at it as if it was a file.

What am I doing wrong? Is this a problem with requests made from iOS devices and sent to Rails servers? Some sort of bug? I've already tried ASIHTTPRequest, with the same results.

1 Answers1

1

I solved the problem with something incredibly simple. I had to remove

Content-Type: application/octet-stream

from the body and I had to add Connection: Keep-Alive to the request, like this:

[request setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];

I tried it after listening to the request on the receiving computer using WireShark and noticing that the connection was set to close as default.