Note that there are some explanatory texts on larger screens.

plurals
  1. PONSURLConnection closes early on GET
    text
    copied!<p>I'm working on a method to centralize my URL connections for sending and receiving JSON data from a server. It works with POST, but not GET. I'm using a Google App Engine server and on my computer it'll handle the POST requests and return proper results (and log appropriately), but I get the following error when I try the request with a GET method: </p> <pre><code>Error Domain=kCFErrorDomainCFNetwork Code=303 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 303.)" UserInfo=0xd57e400 {NSErrorFailingURLKey=http://localhost:8080/api/login, NSErrorFailingURLStringKey=http://localhost:8080/api/login} </code></pre> <p>In addition, the GAE dev server shows a "broken pipe" error, indicating that the client closed the connection before the server was finished sending all data.</p> <p>Here's the method:</p> <pre><code>/* Connects to a given URL and sends JSON data via HTTP request and returns the result of the request as a dict */ - (id) sendRequestToModule:(NSString*) module ofType:(NSString*) type function:(NSString*) func params:(NSDictionary*) params { NSString *str_params = [NSDictionary dictionaryWithObjectsAndKeys:func, @"function", params, @"params", nil]; NSString *str_url = [NSString stringWithFormat:@"%@%@", lds_url, module]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str_url]]; NSData *data = [[NSString stringWithFormat:@"action=%@", [str_params JSONString]] dataUsingEncoding:NSUTF8StringEncoding]; [request setHTTPMethod:type]; [request setHTTPBody:data]; [request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; NSError *error = nil; NSURLResponse *response = nil; NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error]; NSLog(@"Error: %@", error); NSLog(@"Result: %@", [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]); return [result objectFromJSONData]; } </code></pre> <p>A sample call would be:</p> <pre><code>NSDictionary *response = [fetcher sendRequestToModule:@"login" ofType:@"GET" function:@"validate_email" params:dict]; </code></pre> <p>Again, this works with a POST but not a GET. How can I fix this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload