How to sign up through Rest API using AFNetworking 2.0?

和自甴很熟 提交于 2019-12-24 17:48:55

问题


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.


回答1:


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.




回答2:


Use this for parameters:

NSString *token = // Fill token
NSString *name = // Fill Name
NSString *email = // Fill Email
NSString *password = // Fill Password
NSDictionary *params = @[token,@{@"data": @[name,email,password]}];


来源:https://stackoverflow.com/questions/32591313/how-to-sign-up-through-rest-api-using-afnetworking-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!