How to set HTTP request using AFNetworking 2?

13,692

Solution 1

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.

Solution 2

//AFN 2.0 is just support IOS 7,and it's standard use as follow:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://example.com/resources.json" 
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSLog(@"JSON: %@", responseObject)
     }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"Error: %@", error);
     }
];
Share:
13,692
LIAL
Author by

LIAL

Updated on June 08, 2022

Comments

  • LIAL
    LIAL about 2 years

    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.

  • LIAL
    LIAL over 10 years
    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"
  • Rob
    Rob over 10 years
    @LIAL Add manager.responseSerializer = [AFHTTPResponseSerializer serializer]; to let it know that you'll accept text/html response.
  • LIAL
    LIAL over 10 years
    Thanks a lot Rob, it really helps.
  • LIAL
    LIAL over 10 years
    Yep, been already done. Just have to convert row data I got to readable string.
  • dondonhk
    dondonhk about 8 years
    is it the way AFNetworking encourages us to use? The first segment of code is simple but I can't find AFNetworking is teaching this. :(. I just doubt why AFNetworking is not teaching me to use it to send a simple GET request
  • Rob
    Rob about 8 years
    Yes, setting the responseSerializer to match the format of the expected response is the recommended way. The reason AFNetworking is not teaching you to use ATHTTPResponseSerializer because you shouldn't generally write a web service that returns JSON response with a Content-Type of text/html! In LIAL's question, he clearly indicates that he's expecting JSON response, but his web service's response is failing to set the Content-Type header appropriately. If a web service is returning JSON, it should set its headers accordingly (e.g. in PHP, header("Content-Type: application/json")).
  • Rob
    Rob about 8 years
    Notice, though, that this answer is written for AFNetworking 2.x. In later versions, you would use neither AFHTTPRequestOperationManager nor AFHTTPRequestOperation. But the setting of responseSerializer works the same way in AFHTTPSessionManager. But, as I said, if the web service set its response's headers appropriately for the JSON response payload, you wouldn't have to mess with responseSerializer at all.