0

How can i send data to server through URL using POST method.

My data is like below:

  json == {
Signup =     {
    email = test;
    password = 123;
    username = test;
 };
}

My URL is like this:

http://192.168.1.122/~test/sample/index.php/Api/signup

Please suggest me. I am stuck on this from last 2 days. Please help me.

Data format is JSON.

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51

2 Answers2

1

You could do something similar to send a simple post request with JSON Data

  -(void)sendPostData{

 NSString *urlStr = @"http://me.com";
 urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 NSURL *url = [NSURL URLWithString:urlStr];

NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:user.userName,@"username",user.password,@"password",user.email,@"email", nil];
    NSError *error;

   NSData* bodyData = [NSJSONSerialization dataWithJSONObject:info
                                                       options:kNilOptions error:&error];


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:data];
    [request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];


    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *responseFromRequest, NSData *data, NSError *error)
     {
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)responseFromRequest;
         NSInteger code = [httpResponse statusCode];

         }];
}
rustylepord
  • 5,681
  • 6
  • 36
  • 49
0

Have a look at my GitHub repo JWURLConnectionenter link description here, this will help you.

If you'r targeting an API I would also recommend JWRESTClient.

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107