Although there are many related questions, I don't see one that addresses adding multiple key/value pairs to an NSURLRequest.
I want to add a simple username and password to a request. I'm unsure of how to add multiple pairs, and also of the encoding. I get a valid connection and response, but the response indicates it hasn't been able to interpret the request properly.
Here's what I've got. Thanks in advance.
NSURL *authenticateURL = [[NSURL alloc] initWithString:@"https://www.the website.com/authenticate"];
NSMutableURLRequest *authenticateRequest = [[NSMutableURLRequest alloc] initWithURL:authenticateURL];
[authenticateRequest setHTTPMethod:@"POST"];
NSString *myRequestString = @"username=";
[myRequestString stringByAppendingString:username];
[myRequestString stringByAppendingString:@"&"];
[myRequestString stringByAppendingString:@"password="];
[myRequestString stringByAppendingString:password];
NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]];
[authenticateRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[authenticateRequest setHTTPBody: requestData];
[authenticateRequest setTimeoutInterval:30.0];
connection = [[NSURLConnection alloc] initWithRequest:authenticateRequest delegate:self];