0

_users在 Cloudant 的数据库中有用户列表。当我尝试通过 cookie 身份验证向/_sessionAPI 端点发送 POST 请求进行身份验证时,如下所示:

NSString *urlString = @"https://username:password@username.cloudant.com/_session";


NSMutableURLRequest *parseRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
NSMutableDictionary *postDictionary = [[NSMutableDictionary alloc]init];
NSError *error;
[postDictionary setValue:@"userinDB" forKey:@"name"];
[postDictionary setValue:@"passowrdinDB" forKey:@"password"];

NSData *postBody = [NSJSONSerialization dataWithJSONObject:postDictionary options:NSJSONWritingPrettyPrinted error:&error];

[parseRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[parseRequest setHTTPMethod:@"POST"];
[parseRequest setHTTPBody:postBody];

[NSURLConnection sendAsynchronousRequest:parseRequest
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                           if (connectionError) {
                               NSLog(@"error:%@",connectionError);
                           }
                           else {
                               NSLog(@"response:%@",response);
                           }
                       }];

它抛出以下异常:

{"error":"bad_request","reason":"user accounts must have a username"}

谁能帮我吗?

4

1 回答 1

1

您必须使用application/jsoncontent-type 而不是application/x-www-form-urlencoded因为那是您正在使用的。

于 2015-01-09T16:41:20.490 回答