How to post json server i am trying to add dictionary and post data but not getting write response
{"type":"general", "noteText":[{"lineNo":"1","lineText":"patient admited"}]}
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSString *Str = @"general";
NSString *St2r = @"Yogesh";
NSString *Str1 = @"1";
NSMutableArray *tempArray = [[NSMutableArray alloc]init];
NSDictionary *tempDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
Str1, @"lineNo",
St2r, @"lineText",
nil];
[tempArray addObject:tempDictionary];
NSDictionary *tempDict = [[NSDictionary alloc]initWithObjectsAndKeys:tempArray,@"noteText", nil];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:tempDict options:0 error:&error];
//[request setHTTPBody:postData];
NSString *jsonRequest = [NSString stringWithFormat:@"{\"type\":\"%@\"}",Str]; // NSLog(@"Request: %@", jsonRequest);
NSURL *url7 = [NSURL URLWithString:@"URl"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url7];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPBody:postData];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
}];
[postDataTask resume];? Add it at the end of your dataTaskWithRequest. - Vannens[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];and[request setHTTPBody: requestData];Why are you using requestData?[request setHTTPBody:postData];is enough and then add the postData content-length to the request: - Vannens