0

i m new in IOS development. i am requesting to web service which is having POST method and accept two parameters password and confirmation in header. this is url https://abc.xyz.eu/pqr/Api this is the method api/Account/ChangePassword?password={password}&confirmation={confirmation}

how to call this method i have tried lots of code but getting 400,401,404 error

finally i am getting 200 status code in chrome by using basic authentication .now problem is that i am confused how to pass values of basic authentication and header parameters plz help me `

        NSString *UrlWithParameters=[NSString stringWithFormat:@"https://abc.xyz.eu/abc/api/Account/ChangePassword?"];
        NSURL *ServiceURL =[NSURL URLWithString:UrlWithParameters];
        NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:ServiceURL];

        NSString *post = [NSString stringWithFormat:@"password=%@&confirmation=%@",self.txtNewPassord.text,self.txtConPassord.text];
        NSData* postData = [post dataUsingEncoding:NSUTF8StringEncoding];

        [request setHTTPMethod:@"POST"];

        NSString *authValue = [NSString stringWithFormat:@"Basic %@",self.appDelegate.UserPasswordBase64];
        [request setValue:authValue forHTTPHeaderField:@"Authorization"];
        [request setValue:@"application/json" forHTTPHeaderField:@"accept"];
        [request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

        [request setHTTPBody:postData];

        NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

`

2 Answers2

0

If you get 401 error, it means you're unauthorised. API call seems strange because you don't send any authorisation information. If it is not your service try to check this API from browser, CLI, or any REST client at first. If it is your server, check server log, how it tries to route your call. The problem may be in URL encoding, for example.

freele
  • 142
  • 7
  • i have added this before NSString *authValue = [NSString stringWithFormat:@"Basic %@",check]; [serviceRequest setValue:authValue forHTTPHeaderField:@"Authorization"]; check string is username and password with base64 format encoded – zulkar patel Feb 04 '15 at 08:05
  • I really mean it. Try this tool it allows test requests, and set auth headers also - https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo – freele Feb 04 '15 at 08:30
  • It should work in browser. When it will work there, then move to iOS – freele Feb 04 '15 at 08:37
0

Try using AFNetworking, it's the single greatest iOS networking library (and open source!)

With it, this might do the trick:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"password": @"xxx", @"confirmation": @"yyy"};
[manager POST:@"https://abc.xyz.eu/pqr/api/Account/ChangePassword" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
esreli
  • 4,993
  • 2
  • 26
  • 40
  • i am not familiar with AFNetworking . i am learning that but this part is tricky for me. – zulkar patel Feb 04 '15 at 08:14
  • It's the easiest way to make any and all URL requests :) Are you familiar with how to install third party libraries in XCode? – esreli Feb 04 '15 at 08:15
  • yes i can install third party code and also i tried by using Afnetworking but did not get my luck .can you please provide me the link to understand AFNetworking. – zulkar patel Feb 04 '15 at 08:17
  • I have linked to the AFNetworking repo and it has all sorts of working examples. If that doesn't make sense to you I would look here http://stackoverflow.com/questions/10943635/how-do-i-pass-multiple-parameter-in-url – esreli Feb 04 '15 at 18:11