0

When i am using NSURLSession while Posting through the Browser is returning the result as 200 status but when i send it through code in IOS i am getting 500 status code as below.

Response:<NSHTTPURLResponse: 0x14e754240> { URL: urlAPI } { status code: 500, headers {
    "Cache-Control" = private;
    "Content-Length" = 30;
    "Content-Type" = "text/plain; charset=utf-8";
    Date = "Thu, 28 Jan 2016 12:59:10 GMT";
    Server = "Microsoft-IIS/7.5";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} } 

Below is my code

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
   NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

   NSURL * url = [NSURL URLWithString:@"HERE IS MY URL"];
   NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
   NSString * params =@"MY PARAMETERS";
   [urlRequest setHTTPMethod:@"POST"];
   [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

   NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                       completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                         NSLog(@"Response:%@ %@\n", response, error);
                                         if(error == nil)
                                         {
                                             NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                             NSLog(@"Data = %@",text);
                                         }

                                     }];
   [dataTask resume];

This code worked previously but it is throwing error now(API code also not changed),where am i doing wrong.Help me out of this.

Hari krishna
  • 1,142
  • 1
  • 12
  • 29
Android Tutorial
  • 829
  • 1
  • 8
  • 20

2 Answers2

0

Try to send the parameters in NSDictionary:

NSError *error;

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@“[Your SERVER URL”];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

[request setHTTPMethod:@"POST"];
NSDictionary *postData = [[NSDictionary alloc] initWithObjectsAndKeys: @“TestUservalue”, @"name",
                     @“TestPassvalue”, @“password”,
                     nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject: postData options:0 error:&error];
[request setHTTPBody: postData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

}];

[postDataTask resume];
Ramesh_T
  • 1,251
  • 8
  • 19
0

The problem is with the parameters that you are sending to your webserver. They must be properly encoded. I was having the same 500 status code. I was able to fix my problem by changing my post parameters. I removed the _ from my post variables name and it worked.

NSString * params = [NSString stringWithFormat:@"q_id=%@&c_id=%@&agent=%@",self.q_id, self.c_id, agent];

// changed to


NSString * params = [NSString stringWithFormat:@"qid=%@&cid=%@&agent=%@",self.q_id, self.c_id, agent];

Also check out this link on Objective-C encoding

Community
  • 1
  • 1