Search code examples
iphoneiosios4

Sending json data in post request in iphone


I am trying to send JSON data in post request to server which throws error. I am creating data in following format

      NSDictionary *o1 = [[NSMutableDictionary alloc] init];
        [o1 setValue:@"51" forKey:@"merchantProductId"];
        [o1 setValue:@"Big Paulie" forKey:@"name"];
        [o1 setValue:@"1" forKey:@"quantity"];
        NSDictionary *o2 = [[NSMutableDictionary alloc] init];
        [o2 setValue:@"52" forKey:@"merchantProductId"];
        [o2 setValue:@"Paulie" forKey:@"name"];
        [o2 setValue:@"10" forKey:@"quantity"];

        NSMutableArray *pizzas = [NSMutableArray arrayWithObjects:o1, o2, nil];
        NSDictionary *o3 = [[NSMutableDictionary alloc] init];
        [o3 setValue:@"3" forKey:@"merchantId"];
        [o3 setValue:pizzas forKey:@"pizzas"];
        NSMutableArray *orderArray = [NSMutableArray arrayWithObjects:o3, nil];
        NSData *jsData = [NSJSONSerialization dataWithJSONObject:orderArray options:NSJSONWritingPrettyPrinted error:nil];
 NSString *data = [NSString stringWithFormat:@"data=%@",[[NSString alloc] initWithData:jsData                                                                                 encoding:NSUTF8StringEncoding]];

       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:61.12.124.234:60/upload_image/phoneTesting.php"]]];
    [request setHTTPMethod:@"POST"];
   // [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSHTTPURLResponse* urlResponse = nil;
    error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"Response: %@", result);

IN server the I need data should present like

      {
    "pizzas" : [
      {
        "quantity" : "1",
        "merchantProductId" : "51",
        "name" : "Big Paulie"
      },
      {
        "quantity" : "10",
        "merchantProductId" : "52",
        "name" : "Paulie"
      }
    ],
    "merchantId" : "3"
  }

but i am getting

(
    [data] => [
  {
    "pizzas" : [
      {
        "quantity" : "1",
        "merchantProductId" : "51",
        "name" : "Big Paulie"
      },
      {
        "quantity" : "10",
        "merchantProductId" : "52",
        "name" : "Paulie"
      }
    ],
    "merchantId" : "3"
  }
]
)

and when i am trying to send only the jsData it does not send any thing with the requset.

please suggest me how to send the only json data in post request.

i am using xcode4.5


Solution

  • I'm wondering why you do the following line:

    NSString *data = [NSString stringWithFormat:@"data=%@", 
        [[NSString alloc] initWithData:jsData encoding:NSUTF8StringEncoding]];
    

    If it was a GET request, then you'd need a key value pairing, but you're just sending POST data, which doesn't need a key.

    I think that's why you're getting that supercilious [data] = entry. If you make it :

    NSString *data = [NSString stringWithFormat:@"%@",
        [[NSString alloc] initWithData:jsData encoding:NSUTF8StringEncoding]];
    

    does it work ?