1
votes

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);
    }

}];
1
post some code how are you doing this???? - Paresh Navadiya
Where is the [postDataTask resume]; ? Add it at the end of your dataTaskWithRequest. - Vannens
I'm also confused with these lines: [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
You should use AFNetworking library: github.com/AFNetworking/AFNetworking - Nauman Afzaal

1 Answers

0
votes

Consider using AFNetworking perhaps?

You can download the code from here

Use the following code as sample for a post call to server.

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];