问题
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
回答1:
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];
回答2:
Yes You can POST n number of employee details using your Web Service.
回答3:
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.
回答4:
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"
}
]
回答5:
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"
来源:https://stackoverflow.com/questions/41481525/nsmutableurlrequest-post-method-with-multiple-objects