2

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]; 
Don Wilson
  • 2,303
  • 3
  • 26
  • 34

2 Answers2

5

You're not using NSString correctly (your myRequestString, in fact, will read "username="). Instead, try this:

NSMutableString *myRequestString = [NSMutableString stringWithString:@"username="];
[myRequestString appendString:username];
[myRequestString appendString:@"&password="];
[myRequestString appendString:password];

Further to this great answer, just a typical example code:

-(NSString *)buildKeyValuePostString
    {
    NSString *username = @"boss@apple.com";
    NSString *password = @"macintosh";

    NSMutableString *r = [NSMutableString stringWithString:@""];

    [r appendString:@"command=listFileNames"];
    [r appendString:@"&"];

    [r appendString:@"name=blah"];
    [r appendString:@"&"];

    [r appendString:@"user="];
    [r appendString: [username stringByUrlEncoding] ];
    [r appendString:@"&"];

    [r appendString:@"password="];
    [r appendString: [password stringByUrlEncoding] ];

    return r;
    }

and here's the category to do the difficult/annoying job of url encoding

-(NSString *)stringByUrlEncoding
    {
    return (NSString *)CFBridgingRelease(
             CFURLCreateStringByAddingPercentEscapes(
                NULL,
                (CFStringRef)self,
                NULL,
                (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                kCFStringEncodingUTF8)
                );

    // with thanks to http://www.cocoanetics.com/2009/08/url-encoding/
    // modified for ARC use 2014
    }

Hope it helps someone.

Fattie
  • 27,874
  • 70
  • 431
  • 719
donkim
  • 13,119
  • 3
  • 42
  • 47
  • OK - thanks. But even after fixing that my request still is not interpreted correctly. Is the setting of the username and password correctly done and encoded? – Don Wilson Feb 05 '12 at 03:24
  • Hmm, other than that `NSMutableString` part, the rest of the code looks like it should run as you expect. I have code very similar to what you wrote that works fine. – donkim Feb 05 '12 at 03:33
  • NSString *myRequestString = @"h=123"; NSData *myRequestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost:8082/45"]]; [request setHTTPMethod: @"POST"]; [request setHTTPBody:myRequestData]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; – donkim Feb 05 '12 at 03:34
  • Sorry for the formatting.. guess it doesn't work for comments? Anyway, that's my code. Are you sure you the string is created as you expected? I'd do an NSLog just to make sure.. – donkim Feb 05 '12 at 03:35
  • You were correct. I was using an invalid method for appending strings. Should have been using appendString. It's working now. Many thanks. – Don Wilson Feb 05 '12 at 03:47
0

Assuming that you mean that you want to add HTTP header fields to the request, use:

-addValue:forHTTPHeaderField:
Caleb
  • 124,013
  • 19
  • 183
  • 272