-1

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

user2813740
  • 517
  • 1
  • 7
  • 29

5 Answers5

2

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];
Gulliva
  • 488
  • 2
  • 10
  • Accoiding to above question: He has to set data like Employee=1&Class=tes&Comp=test&Type=Fixed with dataUsingEncoding:NSUTF8StringEncoding Encoding. – Dheeraj D Jan 05 '17 at 10:22
  • hello i tried with array of dictionary like above but API do not accept dictionary as per webservice team. they asked me to send like LIST. Objective C do not have LIST , how i can send above data instead of dictionary... – user2813740 Jan 13 '17 at 12:30
  • In Objective C the list is NSMutableArray. But we already wrapped it dictionary in an array `NSArray *uploadArray = @[emp,emp1];` is the short way of writing `NSArray *uploadArray = [NSArray arrayWithObjects:emp1,emp2nil];` – Gulliva Jan 13 '17 at 12:45
  • yes i am too sending data same way...but table did not got updated.... i contacted web service team they are saying they are expecting object in form of LIST, is there any other way ...do i have to convert NSArray to string then to JSOn something.... – user2813740 Jan 13 '17 at 13:28
  • No, the format is correct, jou can validate the result with this code `NSData *postData = [NSJSONSerialization dataWithJSONObject:_uploadArray options:NSJSONWritingPrettyPrinted error:nil]; NSString *tmp = [[NSString alloc]initWithData:postData encoding:NSUTF8StringEncoding]; NSLog(@"%@",tmp);` – Gulliva Jan 13 '17 at 13:44
0

Yes You can POST n number of employee details using your Web Service.

Dheeraj D
  • 4,386
  • 4
  • 20
  • 34
  • hmm i am not from web service team ...we have all different team working .... will take some time asking feedback...but if it does allow ....i just have to send nsmutable array of dictionary in place of string right? – user2813740 Jan 05 '17 at 10:01
  • As you have updated the answer and i can see you can POST n number of employe data – Dheeraj D Jan 05 '17 at 10:10
0

I think, you want to pass the NSArray inside the NSDictionary as a parameter along with the url to server. Try like Hermann Klecker's answer and make changes accordingly:

  [
  {
      "Employee":1,
      "Class":"test 1",
      "Comp":"test 1",
      "Type":"test 1"
   },
  {
      "Employee":2,
      "Class":"test 2",
      "Comp":"test 2",
      "Type":"test 2"
   }
]

Convert array into json string or make dictionary of post data than convert it into json string and post string to server.

Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
  • NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"Employee", @"Class",@"Comp",@"Type", nil]; Where is object?? They all are key – Dheeraj D Jan 05 '17 at 10:20
0

Apparently you will have to form a body like this:

[
  {
      "Employee":1,
      "Class":"test 1",
      "Comp":"test 1",
      "Type":"test 1"
   },
  {
      "Employee":2,
      "Class":"test 2",
      "Comp":"test 2",
      "Type":"test 2"
   }
]
shim
  • 9,289
  • 12
  • 69
  • 108
Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
  • how to form above body.....array of dictionary won't work as per web service team...LIST is not available in Objective C – user2813740 Jan 13 '17 at 12:31
  • Why don't you use an NSArray then or NSMutableArray? That is the closest counterpart in Obj-C to List in Java. – Hermann Klecker Jan 13 '17 at 13:13
  • I guess the shortest option is as follows: `@[@{@"Employee":@1,@"Class":@"test 1",@"Comp":@"test 1",@"Type":@"test 1"},@{@"Employee":@2,@"Class":@"test 2",@"Comp":@"test 2",@"Type":@"test 2"}] ` – Hermann Klecker Jan 13 '17 at 13:16
-1

replace you string body

NSString * strBody = @"Employee = 1 & Class = tes & Comp = test & Type = Fixed"

to

NSString * strBody = @"Employee=1&Class=tes&Comp=test&Type=Fixed"
Vadim Kozak
  • 420
  • 3
  • 11
  • yes i have same ..it was an example... my question is different ..... what if i have to send data of more then one employee.... how object should go...? – user2813740 Jan 05 '17 at 09:56