?
Solved

Add body fields to NSMutableURLRequest

Posted on 2012-03-22
2
Medium Priority
?
1,106 Views
Last Modified: 2012-08-14
How would I add key value pairs to the body of a NSMutableURLRequest? I know I could use something like this:

NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:8080/upload/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *bodyContents = @"fileName=";
[bodyContents stringByAppendingString:fileName];
[bodyContents stringByAppendingString:@"&"];
[bodyContents stringByAppendingString:@"deviceName="];
[bodyContents stringByAppendingString:deviceName];
[bodyContents stringByAppendingString:@"fileContents="];
[bodyContents stringByAppendingString:fileContents];

Open in new window


But that seems like a lot of hassle, especially if I get to where I need to submit a lot of values in a request. I would hope apple would provide a method like

[request addBodyField:@"fileName":fileName] 

Open in new window


or something equally useful, but I haven't been able to find one in the documentation. Is there one I'm missing or should I just stick to appending strings?
0
Comment
Question by:HDM
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 2000 total points
ID: 37756486
Looks like you need:
[request setHTTPBody:data];
where data is an NSData object that can be made for example so:
NSString *text = @"param=first&param=second";
NSData *data = [text dataUsingEncoding:NSASCIIStringEncoding]];

How to Write an iOS App That Uses a Web Service
0
 

Author Comment

by:HDM
ID: 37758484
That's what I'm thinking too, but I don't like building the string manually, but if that's all I've got access to then I guess it's what I'll use
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating an installable image of an iPhone mobile app (an .ipa file) that beta testers could load onto their test devices. The process involved quite a few steps, but both Appcelerator and Diawi.org had decent …
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question