Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7415463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:18:30+00:00 2026-05-29T07:18:30+00:00

Although there are many related questions, I don’t see one that addresses adding multiple

  • 0

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]; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:18:30+00:00Added an answer on May 29, 2026 at 7:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Although there is sample code in the ADC for parent/child (one to many rather
There are many questions that ask how to change the class of a div
Although there are a lot of posts about .net config files, I believe that
Although there are some similar questions I’m having difficulties finding an answer on how
I have the following code that won't compile and although there is a way
I'm building a site using CodeIgniter that largely consists of static content (although there
Well although it's late in the dark night, I don't get why there are
First things first, there are many questions similar to this, I know, but I
I know there exists many questions about this, but I dont know what to
although there are many posts on the internet as well as some posts on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.