0

I have list of users in the _users DB in Cloudant. When I try to authenticate by cookie authentication sending a POST request to the /_session API end point as below:

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);
                           }
                       }];

It is throwing the following exception:

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

Can anyone help me out?

Simon
  • 31,675
  • 9
  • 80
  • 92

1 Answers1

1

You must use the application/json content-type instead of application/x-www-form-urlencoded since that's what you are using.

Simon
  • 31,675
  • 9
  • 80
  • 92
  • Understood i have to post username and password with in url and i need to set content-type:x-www-formurl encoded.so do you have any idea.how to POST with above case. – user3816512 Jan 09 '15 at 17:17
  • You can use JSON, just set the correct content-type. – Simon Jan 09 '15 at 17:26
  • Or if you prefer you can pass the name and password as a query in the URL. Just make sure the URL-encode them properly. – Simon Jan 09 '15 at 17:28
  • I have tried using json but again its throwing same exception and In clodant docs they mentioned to use content type as url encoded.so can you guide me how to pass username and password in url encoded. – user3816512 Jan 09 '15 at 17:47