-1

Hi i am new in ios development i want to send some json data on my web service by http post request where the data will be in request body not in parameter?

Any help is Appreciated , Thanks .

  • 1
    Possible duplicate of [Sending a JSON via POST in NSURLRequest](http://stackoverflow.com/questions/7404559/sending-a-json-via-post-in-nsurlrequest) – M. Porooshani Oct 20 '15 at 06:59

2 Answers2

0

Try this code

    NSURL *url = [NSURL URLWithString:@"Your Json URL"];

     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                               timeoutInterval:60.0];
            [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

            [request setHTTPMethod:@"POST"];

            NSURLResponse *response;
            NSError *err;
            NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
            NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
            NSLog(@"responseData in string : %@",str);

          NSMutableDictionary *dictTemp = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];

NSLog(@"responseData in Dictionary : %@",dictTemp);
DHEERAJ
  • 1,478
  • 12
  • 32
0
> Try this code

    -(void)answerCode
{
    NSError * error ;
    NSURLResponse * urlResponse;

    NSURL * postUrl =[NSURL URLWithString:YourUrl];//enter your url

//your body here 
NSString * body =[NSString stringWithFormat:@"email=dharasis"];



    NSMutableURLRequest * request =[[NSMutableURLRequest alloc]initWithURL:postUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:50];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];

    NSData * data =[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    if (!data) {

        return;
    }

    id json =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

  NSLog(@"Json result is:%@",josn);

}