Solved

Add body fields to NSMutableURLRequest

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

A common question or need, when setting-up a new Mac for someone would be to make all of the applications, installed, available from the dock. Many people often do not realize an application is installed unless it is in the dock. Creating a custo…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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