Hussein Hussein - 2 years ago 182
HTML Question

Generate POST request in iOS using AFNetworking

I was asked to build an iOS app based on Fedena (School management system). Their API sample codes are in HTML-JS or RUBY. What I have to do is to pass the arguments (token, URI, etc..) programatically, and upon sending them, the request will return an XML file which I can then parse and view in a tableView.

  1. How can I pass parameters by code?

  2. How can I fetch the XML file?

This is an example:

<head>'GET', fedena_server+"/api/users/"+username);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('Authorization', 'Token token="'+token+'"');

Answer Source

First you will have to add AFNetworking to your project. The easiest way to do so is using pods info can be found here :

the following code will fetch the response for the mentioned API call:

NSString *url = [NSString stringWithFormat:@"%@/api/users/%@",fedenaServer,username];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *token = [NSString stringWithFormat:@"Token token=%@",token];
[manager.requestSerializer token forHTTPHeaderField:@"Authorisation"];
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // your code to parse the response
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

You'll have to do a bit of research for the whole thing to work, like parsing the XML response Giving you more than that would be doing the job for you :P Hope it helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download