-1

I have a very annoying problem, which I'm trying to solve. I have an iPhone app that sends several forms of data to a server. The server-side processes the data and responds, with PHP. With small lengths of data this goes fine, but one of the requests is pretty large and this doesn't go so well. The requests seems well encoded, with the proper length, even the Content-Length header is set. Still I get an incomplete (literally chopped off) result in PHP.

Here's some code:

NSString *requestString = [NSString stringWithFormat:@"json=%@", [params JSONFragment], nil];
NSLog(@"%@", requestString);
NSLog(@"%u", [requestString length]);
NSData *requestData = [requestString dataUsingEncoding:NSUTF8StringEncoding];
//NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: strlen([requestString UTF8String])]; //[requestString UTF8String]
//NSData *requestData = [NSData dataWithBytes: requestString length: [requestString length]]; //[requestString UTF8String]
//NSData *requestData = [NSData dataWithBytes:[requestString UTF8String] length:[requestString lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"https://radarromance.com/api/index.php"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setValue:[NSString stringWithFormat:@"%u", [requestString length], nil] forHTTPHeaderField:@"content-length"];

Consider the actual data to be a properly formatted JSON string, with a length of > 4000 characters. I ran a tiny test to see whether the NSData object had the correct length, and it has. On the iPhone side, I assume, all is fine.

The PHP side however shows only a part of the request, so it's incomplete. The post_max_size is 16M, which should be sufficient.

How is this possible? What can possibly be wrong? Any help here is greatly appreciated!

Kind regards,

Reinder

Reinder de Vries
  • 522
  • 1
  • 6
  • 26
  • Have you tried sending the request without the content-length header? – Nick Weaver May 25 '11 at 21:50
  • Yes, didn't work! @Rizwan So did changing the Content-Type. @Bitterzoet I checked, the response is almost instant and the timeout is set to an hour... Isn't there a PHP setting or quirk I am overlooking? – Reinder de Vries May 26 '11 at 09:01

2 Answers2

0
@"application/x-www-form-urlencoded"

try with application/json

Rizwan Sharif
  • 1,089
  • 2
  • 10
  • 20
0

I've had this problem as well with scraping websites with other http clients, but maybe the solution is the same. I had incomplete requests because my timeout was set to low and downloading (or posting in your case) took to long so it would result in my requests being incomplete. Might be something worth checking!

Bitterzoet
  • 2,752
  • 20
  • 14
  • I never really solved it, but your answer made me think about other possibilities. If the request/response is really too heavy, it might be clever to cut it in little pieces ;-). – Reinder de Vries Jun 02 '11 at 12:19