Note that there are some explanatory texts on larger screens.

plurals
  1. POAFNetworking - How to make POST request
    text
    copied!<p><strong>EDIT 07/14</strong></p> <p><em>As Bill Burgess mentionned in a comment of his answer, this question is related to the <code>version 1.3</code> of <code>AFNetworking</code>. It may be outdated for the newcomers here.</em></p> <hr> <p>I'm quite new to iPhone development, and I'm using AFNetworking as my services library.</p> <p>The API i'm querying is a RESTful one, and I need to make POST requests. To do this, I tried with the following code :</p> <pre><code>NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil]; NSURL *url = [NSURL URLWithString:@"http://localhost:8080/login"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { NSLog(@"Pass Response = %@", JSON); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { NSLog(@"Failed Response : %@", JSON); }]; [operation start]; </code></pre> <p>There are two main issues with this code :</p> <ul> <li><code>AFJSONRequestOperation</code> seems to make a <code>GET</code> request, not a <code>POST</code> one.</li> <li>I can't put parameters to this method.</li> </ul> <p>I also tried with this code :</p> <pre><code>NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil]; NSURL *url = [NSURL URLWithString:@"http://localhost:8080"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; [httpClient postPath:@"/login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Succes : %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Failure : %@", error); }]; </code></pre> <p>Is there a better way to make what I want here to get it done ?</p> <p>Thanks for the help !</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