KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to post data to a PHP web service. I am familiar doing this in html using query $.post but I am very much stumped trying this in objective C. I tried several blogs & questions found on stackoverflow. I finally came up with the following code: NSString *jsonRequest = [NSString stringWithFormat:@"{\"Email\":\"%@\",\"FirstName\":\"%@\"}",user,fname]; NSLog(@"Request: %@", jsonRequest); NSURL *url = [NSURL URLWithString:@"http:myurl..."]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; [request setHTTPMethod:@"POST"]; [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]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; I also tried: NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; My web service creates a new user on post and returns the userID. Both successfully make the web service call(a new userID is created), however they do not post the data, i.e. a blank user is created every time. Please tell me if I am missing anything. Thanks. My other attempts: NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"myUrl.. "]]; [request setHTTPMethod:@"POST"]; NSString *postString = @"Email=me@test.com&FirstName=Test"; [request setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"]; [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]]; [[NSURLConn
Tags (comma-separated)
Save Edits
Cancel