How to send form data in post request in iOS?
14,487
You need to convert the JSON String to NSData Format. Then you would be able to set this data to HTTP Body in URLRequest object.
Adding code sample:
NSData* postData= [<yourJSON> dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
Let me know in case of any more help...
Author by
Joey Franklin
Updated on June 04, 2022Comments
-
Joey Franklin almost 2 years
Possible Duplicate:
POST in Objective-C iPad devI'm very inexperienced with networking and the like, so please be kind.
I'm trying to send a post request with the following form data
{"email":"[email protected]","password":"password","password_confirm":"password","name":"Joe Smith","cellphone":"4402415585","address":"Fake Street"}:
So far here is what I have:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",SERVER_ADDRESS]] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]; [request setHTTPMethod: @"POST"]; NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; [connection start];
My issue is I can't figure out how to include the form data in this request. I'm sure there's a simple method of
NSMutableURLRequest
that can be used, but I haven't been able to figure out which one.