2015-01-09 57 views
0

我在Cloudant的_users DB中擁有用戶列表。當我嘗試通過cookie的身份驗證來驗證發送POST請求,如下/_session API終點:Cloudant拋出異常的Cookie驗證

NSString *urlString = @"https://username:[email protected]/_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"} 

誰能幫助我嗎?

回答

1

您必須使用application/json內容類型而不是application/x-www-form-urlencoded,因爲這正是您正在使用的內容。

+0

瞭解我必須在URL中發佈用戶名和密碼,我需要設置內容類型:x-www-formurl encoded.so您是否有任何idea.how POST與上述情況。 – user3816512

+0

您可以使用JSON,只需設置正確的內容類型即可。 – Simon

+0

或者,如果您願意,可以在URL中傳遞名稱和密碼作爲查詢。只要確保URL編碼正確。 – Simon