Solved

Add body fields to NSMutableURLRequest

Posted on 2012-03-22
2
1,101 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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

iCloud Drive was introduced after iOS 8 was launched last year. This drive is Apple’s online storage device that lets users sync their files and access them from all their Apple devices.   There is a lot of data that is not automatically backed up…
Set up iPhone and iPad email signatures to always send in high-quality HTML with this step-by step guide.
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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