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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:18:17+00:00 2026-06-18T11:18:17+00:00

I am attempting to write a method/function in objective c to post data to

  • 0

I am attempting to write a method/function in objective c to post data to an online php script. All is going well, Its successfully uploads as many files as I provide it with but I cannot figure out to send variables along with these files. I’ve spent hours now looking through this site and I am unable to find an example that posts files as well as variables.

NSURL * postURL = [NSURL URLWithString:serverURL];
NSString * contentBoundary = @"-14737809831466499882746641449";
NSString * contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", contentBoundary];

// Build Request
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:postURL];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData * postBody = [NSMutableData data];

NSMutableString * postString = [NSMutableString stringWithFormat:@""];
for(NSString * key in dataArray) [postString appendString:[NSString stringWithFormat:@"%@=%@&", key, [dataArray objectForKey:key]]];
[postString deleteCharactersInRange:NSMakeRange([postString length] -1, 1)]; // Trim trailing ampersand from string
NSData * postData = [postString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];

[postBody appendData:[NSData dataWithData:postData]];

if(attachments != nil){
    for(NSString * filePath in attachments){
        NSString * fileName = [filePath lastPathComponent];
        NSData * attachedFile = [NSData dataWithContentsOfFile:filePath];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", contentBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition:form-data;name=\"userfile[]\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[[NSString stringWithFormat:@"Content-Type:application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [postBody appendData:[NSData dataWithData:attachedFile]];
        [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", contentBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    }
}

NSString * postLength = [NSString stringWithFormat:@"%d", [postBody length]];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPBody:postBody];

Any help would be appreciated.

  • 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-18T11:18:19+00:00Added an answer on June 18, 2026 at 11:18 am

    You can try appending the variable’s values to the posted data in the following manner :

    // text parameter
        [postbody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
        [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"applicationId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *parameterValue1=[dict objectForKey:@"applicationId"];
        [postbody appendData:[parameterValue1 dataUsingEncoding:NSUTF8StringEncoding]];
        [postbody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    

    Hope that works for you. Above code is just an example of the parameter sent with the file upload.

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

Sidebar

Related Questions

I am attempting to write a method that has an optional EventHandler Paramater. it
I am curretnly attempting to write a script in python that allows me to
I have been attempting to write a VBA Script that can parse out other
I'm attempting to write a very basic script using javascript. What I want to
I'm attempting to use the method of internal storage to retain my data. In
So I'm attempting to write a simple sprite loading method using a canvas, and
I am attempting to write a custom control. I want to allow its user
I'm attempting to write a simple Select method on a class that inherits from
I'm attempting to write a thread-safe method which may only be called once (per
I'm attempting to write a menu driven modular perl script that will capture user

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.