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 9125933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:55:10+00:00 2026-06-17T06:55:10+00:00

I have the following method that I use to create a NSMutableURLRequest with custom

  • 0

I have the following method that I use to create a NSMutableURLRequest with custom post data contained within a NSMutableDictionary.

+ (NSMutableURLRequest *)createURLRequestWithURL:(NSString *)URL andPostData:(NSMutableDictionary *)postDictionary {
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]init];
    NSString *postString = @"";
    NSString *postLength = nil;
    NSData *postData = nil;

    //convert post distionary into a string
    if (postDictionary) {
        for (NSString *key in postDictionary) {
            if ([postString length] != 0) {
                postString = [postString stringByAppendingString:@"&"];
            }

            postString = [postString stringByAppendingFormat:@"%@=%@", [self urlEncodeString:key], [self urlEncodeString:[postDictionary valueForKey:key]]];
        }
    }

    //get length of post and convert post into a data object
    postLength = [NSString stringWithFormat:@"%d", [postString length]];
    postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    //setup the request
    [urlRequest setURL:[NSURL URLWithString:URL]];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPBody:postData];

    return urlRequest;
}

Up until this point I’ve been using only string data as the values in the postDictionary.

My question is how should I modify this code to send other kinds values? Specifically I’m trying to send images to the server.

  • 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-06-17T06:55:11+00:00Added an answer on June 17, 2026 at 6:55 am

    Used the info from the other answers to modify my code as needed:

    + (NSMutableURLRequest *)createURLRequestWithURL:(NSString *)URL andPostData:(NSMutableDictionary *)postDictionary {
        NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]init];
        NSMutableData *postData = [NSMutableData data];
    
        NSString *boundary = @"---------------------------14737809831466499882746641449";
    
        //convert post distionary into a string
        if (postDictionary) {
            for (NSString *key in postDictionary) {
                [postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
                id postValue = [postDictionary valueForKey:key];
    
                if ([postValue isKindOfClass:[NSString class]]) {
                    [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name= \"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
                    [postData appendData:[postValue dataUsingEncoding:NSUTF8StringEncoding]];
                } else if ([postValue isKindOfClass:[UIImage class]]) {
                    [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@.png\"\r\n", key, [TSGUIDCreator generateGUID]] dataUsingEncoding:NSUTF8StringEncoding]];
                    [postData appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                    [postData appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                    [postData appendData:UIImagePNGRepresentation(postValue)];
                } else {
                    [NSException raise:@"Invalid Post Value" format:@"Received invalid post value while trying to create URL Request. Post values are required to be strings. The value for the following key was not a string: %@.", key];
                }
            }
    
            [postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        }
    
        //setup the request
        [urlRequest setURL:[NSURL URLWithString:URL]];
        [urlRequest setHTTPMethod:@"POST"];
        [urlRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forHTTPHeaderField:@"Content-Type"];
        [urlRequest setHTTPBody:postData];
    
        return urlRequest;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method that returns void and I need to use it
I have a program that makes use of the following method to get a
I have a method that I will use in the following contexts: 1. User
I have the following static method that prints the data imported from a 40.000
I have the below LINQ method that I use to create the empty EmploymentPLan
I have the following link: <a href=/kits?resource_id=536 class=authorize spin data-method=post data-remote=true id=use_it_link_171 rel=nofollow> After
I have the following method that should create a 20 character ID (sometimes with
I have the following method that I use to serialize various objects to XML.
I have the following method that is replacing a pound sign from the file
I have the following method that is supposed to be a generic Save to

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.