Solved

Add body fields to NSMutableURLRequest

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

When Apple released Swift last year, the aim was to introduce a new programming language for Cocoa and Cocoa Touch that was fast, easy and effective, like the name connotes. Apple succeeded. Swift is designed to couple with Objective-C program…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now