0

Hi i am very new for ios and in my app i have used ASIFormDataRequest earlier days for integrating the services

now i have changed format and i am using NSURLSession instead of ASIFormDataRequest

But when i request change password to server using ASIFormDataRequest success response is coming from server but when i use NSURLSession failed response coming from server please help what is wrong

NSURlsession:-

def = [NSUserDefaults standardUserDefaults];
 NSString *myString = [def stringForKey:@"AccesToken"];
 NSString *AccessToken = [NSString stringWithFormat:@"Bearer %@",myString];

 NSString *Finalstr = [NSString stringWithFormat: @"MedicaidId=%@&OldPassword=%@&NewPassword=%@&ConfirmPassword%@", medicaId,self.CurPwdTxt.text,self.NewPwdTxt.text,self.ConfPwdTxt.text];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:mainurl,BaseURL]]

                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy

                                                       timeoutInterval:60.0];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[Finalstr dataUsingEncoding:NSUTF8StringEncoding]];
    [request addValue:AccessToken forHTTPHeaderField:@"Authorization"];

    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {


        if (error) {

            NSLog(@"dataTaskWithRequest error: %@", error);
        }

        else if ([response isKindOfClass:[NSHTTPURLResponse class]]) {

            NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];

            if (statusCode != 200) {

                NSError *parseError;
                id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

                 NSLog(@"responseobject is %@",responseObject);

            }else{

                NSError *parseError;

                id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];

                NSLog(@"else condtion");

                if (!responseObject) {

                    NSLog(@"JSON parse error: %@", parseError);

                    NSLog(@"responseobject is%@",responseObject);

                } else {


                    NSLog(@"responseobject is %@",responseObject);

                }

                //if response was text/html, you might convert it to a string like so:

                NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(@"final responseString = %@", responseString);
            }
        }
    }];

    [task resume];
}

ASIFormDataRequest:-

NSString *urlStr = [NSString stringWithFormat:@"myurl",BaseURL];
    NSLog(@"urlStr --->>> %@",urlStr);

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [request setRequestMethod:@"POST"];

    def = [NSUserDefaults standardUserDefaults];
    NSString *myString = [def stringForKey:@"AccesToken"];

    NSString *str = [NSString stringWithFormat:@"Bearer %@",myString];
    NSLog(@"token is %@",str);

    [request setPostValue:medicaId forKey:@"MedicaidId"];
    [request setPostValue:self.CurPwdTxt.text forKey:@"OldPassword"];
    [request setPostValue:self.NewPwdTxt.text forKey:@"NewPassword"];
    [request setPostValue:self.ConfPwdTxt.text forKey:@"ConfirmPassword"];
    [request addRequestHeader:@"Authorization" value:str];

    [request setDelegate:self];
    [request startAsynchronous];
Pratik Shah
  • 563
  • 4
  • 14
AbhiRam
  • 2,033
  • 7
  • 41
  • 94

1 Answers1

0

You are sending different requests there. In the 'new' code, you are constructing the POST data like this:

@"MedicaidId=%@&OldPassword=%@&NewPassword=%@&ConfirmPassword%@"

which looks like GET-parameters more than form-encoding (which you use on the 'old' request).

Tracking your request and checking out the actual request using something like Wireshark might help you out to spot the problem yourself next time.

Tobi Nary
  • 4,566
  • 4
  • 30
  • 50
  • i am not undestand what ur saying for sending parameters using this form in NSurlsession – AbhiRam Mar 03 '16 at 09:53
  • What you do put in the body is not the right data. See [this Question](https://stackoverflow.com/questions/19099448/send-post-request-using-nsurlsession?rq=1). – Tobi Nary Mar 03 '16 at 09:58
  • see there @EridB answer in provided question by u that's what i followed – AbhiRam Mar 03 '16 at 10:01
  • Please educate yourself on form encoding. And match what you are sending to what the server is expecting. – Tobi Nary Mar 03 '16 at 10:03