Search code examples
iosobjective-cafnetworking-2

How to sign up through Rest API using AFNetworking 2.0?


I have to make parameter like this

array( 
‘token’,
‘data’ => array( ‘name’,
                 ‘email’,
                 ‘password’,
  ) 
)

As I am a beginner to use REST API service. So I can't able to make this. I tried the following way to request

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"name" : name,
                             @"email" : email,
                             @"password" : password};
    [manager POST:BASE_URL  parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

Got a respose like this

Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7f965071adc0 {AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7f9650721f80> { URL: BASE_URL } { status code: 200, headers {
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Type" = "text/html";
    Date = "Tue, 15 Sep 2015 16:33:10 GMT";
    Server = "nginx/1.8.0";
    "Transfer-Encoding" = Identity;
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, NSErrorFailingURLKey=BASE_URL

Is there any mistake to POST the request for sign up? What is the correct way to request? Please help.


Solution

  • Try this

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //edited
       NSDictionary *innerDict = @{@"name":name,@"email":email,@"password":password};
    NSDictionary *dict = @{@"data":innerDict};
    NSArray *parameter = @[@token,@dict];
    [manager POST:BASE_URL  parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//add this line
    

    Include the last line i added.There might be some syntax error coz i am in a windows machine now, but xcode will auto complete for you, if you write it down on your own.

    That should do it now.