Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Where's JSON in your example? You have nothing looking like it in the example. You're doing a few things wrong in setting up the request, check Sulthan answer.</p> <p>My advice is to use a library that handles such minor formal details - like encoding and headers - for you.</p> <p>With <a href="https://github.com/AFNetworking/AFNetworking" rel="nofollow"><code>AFNetworking</code></a> you could write something like.</p> <pre><code>AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://whatever.com/"]]; [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; [httpClient setParameterEncoding:AFFormURLParameterEncoding] NSDictionary * params = @{ @"j_username": nombre, @"j_password": pass }; NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"relative/path/to/resource" parameters:params]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; [httpClient enqueueHTTPRequestOperation:operation]; </code></pre> <p>(example based on <a href="http://samwize.com/2012/10/25/simple-get-post-afnetworking/" rel="nofollow">http://samwize.com/2012/10/25/simple-get-post-afnetworking/</a>)</p> <p>While in terms on LOC in may look not so great, consider that:</p> <ul> <li><code>httpClient</code> is initialized just once and you can reuse it for subsequent requests, centralizing the configuration</li> <li>parameters gets automatically encoded in the desired format and if you have to change the encoding in the future you just have to change <code>AFFormURLParameterEncoding</code> to something else.</li> <li>you get a nice block-based API instead of relying on the cumbersome <code>NSURLConnectionDelegate</code> methods</li> </ul>
 

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