Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Below is my POST request example which saves Employee details. This work fine and I'm sending individual employee details.

But what if I have to save more then one Employee details...do I have to call below method those many time's ... How can I send all data in individual object like nsmutablearray of nsmutable dictionary....

-(void)saveEmployDetails
{
  NSString * strBody = @"Employee=1&Class=tes&Comp=test&Type=Fixed";

        NSString *strUrl = [[NSString alloc] initWithFormat:@"%@/api/external/SaveEmployee?type=%@", strCompURL, strType];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
        request.HTTPMethod = @"POST";
        request.HTTPBody = [strBody dataUsingEncoding:NSUTF8StringEncoding];
        request.timeoutInterval = 5;
        NSURLSessionDataTask *task = [sessionMnger dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
          {
              [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
              if (!error) {
                  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                  if (httpResponse.statusCode == 200)
                  {
                      NSDictionary *jsonData = [NSJSONSerialization   JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
                      NSLog(@"data:%@", jsonData);
                  }
              }
              else
                  NSLog(@"Error:%@", error.description);

          }];
        [task resume];
}

Webservice Team gave me body for above API POST call like

This is a Post Method and below is the Body Object
    [
      {
     "Employee":937,
     "Class":test,
     "Comp":test,
     "Type":test
       }
    ]

How to send more then one employee details together in above API

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
117 views
Welcome To Ask or Share your Answers For Others

1 Answer

So the Webservice accepts json. Just create an NSDictionary like this:

NSDictionary *emp = @{@"Employee":[NSNumber numberWithInt:1],
                      @"Class":@"Test",
                      @"Comp":@"Test",
                      @"Type":@"test"};

NSDictionary *emp1 = @{@"Employee":[NSNumber numberWithInt:1],
                      @"Class":@"Test2",
                      @"Comp":@"Test2",
                       @"Type":@"test2"};


NSArray *uploadArray = @[emp,emp1];

NSString *strUrl = [[NSString alloc] initWithFormat:@"%@/api/external/SaveEmployee?type=%@", strCompURL, strType];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
request.HTTPMethod = @"POST";
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:uploadArray options:NSJSONWritingPrettyPrinted error:nil]];
request.timeoutInterval = 5;
NSURLSessionDataTask *task = [sessionMnger dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                              {
                                  [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
                                  if (!error) {
                                      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                                      if (httpResponse.statusCode == 200)
                                      {
                                          NSDictionary *jsonData = [NSJSONSerialization   JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
                                          NSLog(@"data:%@", jsonData);
                                      }
                                  }
                                  else
                                      NSLog(@"Error:%@", error.description);

                              }];
[task resume];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...