-3

Posting request to .NET server is not working.

NSDictionary *jsonDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSURL *postURL = [NSURL URLWithString: @"SOME URL"];
NSError *error=nil;
NSData *jsonData = [NSJSONSerialization  dataWithJSONObject:jsonDict options:0 error:&error];


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: postURL
                                                       cachePolicy: NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval: 60.0];

[request setHTTPMethod: @"POST"];
[request setValue: @"application/json" forHTTPHeaderField: @"Accept"];
[request setValue: @"application/json" forHTTPHeaderField: @"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];

NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
                                                encoding:NSUTF8StringEncoding]);

The Output is:

{"CreatedBy":"","EmailAddress":"devanrajupericherl@gmail.com","Zipcode":"","FirstName":"devan","DeviceCategoryID:":"","State":"","CustomerTypeID:":"",..........} like this.....

But it Json object must be:

({CreatedBy:"",EmailAddress:"devanrajupericherl@gmail.com",Zipcode:"",FirstName:"devan",DeviceCategoryID::"",State:"",CustomerTypeID:""  })

I am using .Net server for Posting.

Request is not Posting to the Server. Anyone please help me.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
  • 1
    The output looks correct. Please spend 10 minutes and learn [JSON format](http://www.json.org). If you are unable to parse this json on server side, the problem lies elsewhere. – maroux May 03 '13 at 11:05
  • 1
    You can always use http://json.parser.online.fr/ to verify your output. – maroux May 03 '13 at 11:07

1 Answers1

0
NSString *urlString = @"Your Web Service URL";

NSString *parameterString = @"XYZ";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSMutableData *body = [NSMutableData data];

//  parameter
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"parameter_key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:parameterString dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


// setting the body of the post to the reqeust
[request setHTTPBody:body];

// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"returnString %@",returnString);
Gaurav Rastogi
  • 2,145
  • 1
  • 14
  • 19