Ios – How to set HTTP request using AFNetworking 2

afnetworking-2iosobjective crequest

I need to send ordinary HTTP request (GET) and answer will in text/html.
How can I send this response using AFNetworkin 2 ?

Now I'm trying to use

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://example.com"]];
[self HTTPRequestOperationWithRequest:request
                              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                  NSLog(@"JSON: %@", responseObject);
                              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                  NSLog(@"Error: %@", error);
                              }];

And was frustrates – it do nothing. When debugging, nor success nor fail clause have been triggered.

Also I tried to use GET:parameters:success:failure: method, but in response I see this error:

Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request
failed: unacceptable content-type: text/html"

Please, anybody can explain me what are wrong and what is the correct way to send request (if I will get response as text/html)?

Regards, Alex.

Best Answer

You said in your comment, in response to the suggestion to use the AFHTTPRequestOperationManager:

When I used GET I got this error as I wrote above: Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html"

You can remedy that with a AFHTTPResponseSerializer:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:@"https://example.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // do whatever you'd like here; for example, if you want to convert 
    // it to a string and log it, you might do something like:

    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

You can also use AFHTTPRequestOperation:

NSOperationQueue *networkQueue = [[NSOperationQueue alloc] init];
networkQueue.maxConcurrentOperationCount = 5;

NSURL *url = [NSURL URLWithString:@"https://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    // do whatever you'd like here; for example, if you want to convert 
    // it to a string and log it, you might do something like:

    NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"%@", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
}];
[networkQueue addOperation:operation];

Ideally, though, it's advisable to write server code that returns JSON (or XML), as that's much easier for an app to consume and parse.