Parse REST API uploading binary image data in Objective-C - objective-c

I am getting a 500 error back when trying to send image data to the REST API
NSMutableURLRequest *parseRequest = [NSMutableURLRequest requestWithURL:url];
[parseRequest setHTTPMethod:#"POST"];
[parseRequest setValue:#“*****” forHTTPHeaderField:#"X-Parse-Application-Id:"];
[parseRequest setValue:#“*****” forHTTPHeaderField:#"X-Parse-REST-API-Key:"];
[parseRequest setValue:#"image/png" forHTTPHeaderField:#"Content-Type:"];
NSLog(#"All Headers: %#",[parseRequest allHTTPHeaderFields]);
NSData *imageData = [[NSData alloc]initWithData:[NSData dataFromBase64String:data]];
[parseRequest setHTTPBody:imageData];
NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSError *theJSONError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:parseRequest returningResponse:&theResponse error:&theError];
NSString *responseString = [[NSString alloc]initWithData:theResponseData encoding:NSUTF8StringEncoding];
NSDictionary *dataDictionaryResponse = [NSJSONSerialization JSONObjectWithData:theResponseData options:NSJSONReadingAllowFragments error:&theJSONError];
NSLog(#“Url to send request: %#“,url);
NSLog(#“Response String: %#",responseString);
NSLog(#"Response Error: %#",theError);
This returns :
All Headers:
{
"Content-Type:" = "image/png";
"X-Parse-Application-Id:" = *****;
"X-Parse-REST-API-Key:" = *****;
}
Url to send request: https://api.parse.com/1/files/pic2.png
Response String: *HTML page with 500 error*
Response Error: (null)
I'm assuming that it doesn't like the binary data being passed as the HTTPBody but as I'm not getting any error messages back it's hard to tell what's going on. Making the same request with CURL works fine.

try like this
NSMutableURLRequest *parseRequest = [NSMutableURLRequest requestWithURL:url];
[parseRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[parseRequest setHTTPShouldHandleCookies:NO];
[parseRequest setTimeoutInterval:30];
[parseRequest setHTTPMethod:#"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[parseRequest setHTTPBody:body];
// set URL
[parseRequest setURL:requestURL];

Related

NSURLRequest properly format multipart/form-data

I've created an NSURLRequest that's firing off requests OK. I've got it wired to requestbin, but instead of requestbin parsing the pertinent form data, it's seeing it as raw body.
I've got a working CURL command that does this correctly, and as far as I can tell both request raw bodies look the same -- I can't figure out what in the Obj-c I need to change to get those form parameters to work properly.
Here's the inspect link for the requestbin: http://requestb.in/1iszums1?inspect
Working CURL:
curl -X POST -H "Authorization: Bearer token" -H "X-ADN-Pretty-JSON: 1" -F "type=com.example.test" -F "content=#/Users/me/Desktop/test.jpg" "http://re
questb.in/1iszums1"
Obj-c
NSString* url = #"http://requestb.in/1iszums1";
UIImage* img = [UIImage imageNamed:#"test.jpg"];
NSData *imageData = UIImageJPEGRepresentation(img, 1.0f);
NSString* token = #"token";
NSString* name = #"test.jpg";
NSString* type = #"com.example.test";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"------------------------------6c491b1c62c8";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
[request setValue:[NSString stringWithFormat:#"Bearer %#", token] forHTTPHeaderField:#"Authorization"];
[request setValue:#"1" forHTTPHeaderField: #"X-ADN-Pretty-JSON"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:#"%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"type\"\r\n\r\n%#\r\n", type] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"content\"; filename=\"%#\"\r\n", name] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imageData) {
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(#"Error: %#", error);
} else {
NSString *responseBody = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"%# %#", response, responseBody);
}
}];
Not having tried it, my guess is that you are missing a couple of dashes on your boundary:
change
[body appendData:[[NSString stringWithFormat:#"%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
to
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
Its tough to keep all of the dashes figured out. If your boundary string as declared in the content type is "foo" then you will need to separate each part with "--foo" and end the whole form data with "--foo--"

NSData POST HttpRequest error Objective C

I want to send a document that I obtained as NSData with DocuSign API to a web that has a service to do it.
The service is
POST /api/v1/rest/groups/[:idgroup]/documents/upload/[:oauthtoken]
and the required parameters are:
file --> The file to upload (Type: inputstream)
fileName --> The name of the new file to upload (Type: string)
length --> The size of the file to upload in bytes (Type: long)
I'm trying to do the request with this example Uploading Image via POST in Objective C but I have a service error that says "InvalidParametersServiceApiException"
My code is:
NSString *filename = #"docusignTest.pdf";
NSMutableURLRequest* request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"fileName\"; \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n%#",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"length\"; \r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n%lu",(unsigned long)oResponseData.length] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:oResponseData]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
Here's a suggestion: Instead of ignoring the response and the error, ask sendSynchronousRequest to supply them both to you, then examine what you are getting. It's rather mad to send a POST request without any error handling. The data you sent might be wrong, and servers are notorious about wanting you to get it right, but good servers will tell you what is wrong. The server may have problems. The URL may be wrong. The device may not have a connection.
And posting exactly what you received, without any interpretation by you, might help. Reading the documentation for the service you are calling might also help. No two services are the same.
The example of this link is very helpul.
NSString *filename = #"docusignTest.pdf";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file\"; filename=\"%#\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:oResponseData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"fileName\"\r\n\r\n%#", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"length\"\r\n\r\n%d", oResponseData.length] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSURLResponse *response;
NSError *error2;
NSData *oResponseData2 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error2];
NSMutableString *jsonResponse2 = [[NSMutableString alloc] initWithData:oResponseData2 encoding:NSUTF8StringEncoding];

Twitter API 1.1 - update_with_media returns 500

I'm trying to upload a picture on Twitter from a Mac app, using REST API 1.1 (url: https://api.twitter.com/1.1/statuses/update_with_media.json), but I get always error code 500 and {"errors":[{"message":"Internal error","code":131}]}.
If I upload only a tweet (using /update.json) it works fine every time.
Here is my code:
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1.1/statuses/update_with_media.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f];
[request setHTTPMethod:#"POST"];
[request setHTTPShouldHandleCookies:NO];
NSString *boundary = #"64F3EC90-E32B-4BD9-ADB4-E1A9FBE4AFD6";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
[self signRequest:request]; // Adding Oauth
NSMutableData *body = [NSMutableData dataWithLength:0];
// Adding tweet string
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *data = [[NSString stringWithFormat:#"%#\r\n",#"Uploading again test3"]dataUsingEncoding:NSUTF8StringEncoding];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n",#"status"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];
// Adding image
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"media[]\"; filename=\"test.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Transfer-Encoding: binary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Set HTTPBody
[request setValue:#(body.length).stringValue forHTTPHeaderField:#"Content-Length"];
request.HTTPBody = body;
I had luck with the following code:
- (void)postTweet:(NSString *)tweetString withImageData:(NSData *)imageData {
NSURL *baseURL = [NSURL URLWithString:url_statuses_update_with_media];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[#"status"] = tweetString;
params[#"media[]"] = imageData;
[self sendPOSTRequestForURL:baseURL andParams:params];}
-(NSError *)sendPOSTRequestForURL:(NSURL *)url andParams:(NSDictionary *)params {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0f];
[request setHTTPMethod:#"POST"];
[request setHTTPShouldHandleCookies:NO];
NSString *boundary = #"64F3EC90-E32B-4BD9-ADB4-E1A9FBE4AFD6";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
[self signRequest:request];
NSMutableData *body = [NSMutableData dataWithLength:0];
for (NSString *key in params.allKeys) {
id obj = params[key];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData *data = nil;
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n",key] dataUsingEncoding:NSUTF8StringEncoding]];
if ([obj isKindOfClass:[NSData class]]) {
[body appendData:[#"Content-Type: application/octet-stream\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
data = (NSData *)obj;
} else if ([obj isKindOfClass:[NSString class]]) {
data = [[NSString stringWithFormat:#"%#",(NSString *)obj]dataUsingEncoding:NSUTF8StringEncoding];
}
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:data];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:#(body.length).stringValue forHTTPHeaderField:#"Content-Length"];
request.HTTPBody = body;

Uploading array of images(wrapped in NSData) to the server in iOS

Good day,
I'm trying to upload an array of images to the server, images are wrapped into NSData, but the server returning only one image, this is my code:
- (void)actionSave{
NSArray *imagesArray = [NSArray arrayWithObjects:[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageOne.image)],
[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageTwo.image)],
[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageThree.image)],
[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageFour.image)], nil];
NSData *images = [NSKeyedArchiver archivedDataWithRootObject:imagesArray];
NSMutableURLRequest *requestImg = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#&mid=%#", SEND_POST_TO_WALL, [self.currentFriend objectForKey:#"uid"]]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
[requestImg setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[requestImg addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *postData = [NSMutableData dataWithCapacity:[images length] + 512];
[postData setData:images];
NSMutableData *body = [NSMutableData data];
//Photo
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:postData];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Text
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"text\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[self.textView.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[requestImg setHTTPBody:body];
NSURLConnection *connectionForImage = [NSURLConnection connectionWithRequest:requestImg delegate:self];
if (connectionForImage) {
self.infoData = [NSMutableData data];
} else {
NSLog(#"Connection failed");
}
}
This is the result that server returns:
array(3) {
["act"]=>
string(12) "comments.add"
["module"]=>
string(7) "account"
["mid"]=>
string(4) "7728"
}
POST:
array(1) {
["text"]=>
string(5) "Gjdgy"
}
FILES:
array(1) {
["image"]=>
array(5) {
["name"]=>
string(9) "image.png"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/php9oudKt"
["error"]=>
int(0)
["size"]=>
int(463652)
}
}
What I'm doing wrong???
Thanks in advance!
You showed us the POST of the data but not the GET of that data again, so not sure what you mean by "only returns one image". What its returning should be the exact same data blob you went up, which is the archive. You can test this by logging the size of what you send up with the size of what you get back - they should both be the same if the server is returning your original blob back (if its not, that's a web issue not an ios issue).
With the blob you get back from the web, you then need to NSKeyUnarchive it to get your original array of data items. The unraveling is the reverse of the raveling.

How to send JSON request to service with parameters in Objective-C

I'm creating a iPhone app, and im trying to figure out how to create a JSON request to the webservice that contains parameters. In Java this would look like this
HashMap<String, String> headers = new HashMap<String, String>();
JSONObject json = new JSONObject();
json.put("nid", null);
json.put("vocab", null);
json.put("inturl", testoverview);
json.put("mail", username); // something#gmail.com
json.put("md5pw", password); // donkeykong
headers.put("X-FBR-App", json.toString());
The header has to contain a JSON object and "X-FBR-App" before the service recognizes the request. How would this be implemented in Objective-C ?
USE Post method.try this
NSString *method = #"addProduct";
NSString *jsonstring=#"http://yourdomain.com/product_review_api/product-review.php?";
NSString *urlString = [NSString stringWithFormat:#"%#",jsonstring];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
// image file
NSData *imageData = UIImageJPEGRepresentation( labelimage, 0.8);
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
// parameter method
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[method dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// parameter categoryid
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"category_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[selectedID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
NSDictionary *profile = (NSDictionary*)[returnString JSONValue];
NSLog(#"profile=%#", profile);
you can append all the parameters like this.
JSON isn't baked into iOS, but if you look at www.JSON.org there are a number of Objective-C frameworks that provide the required functionality. I have no experience with any of them so I can't make recommendations, but it is a good start to finding a solution.
Also look at this SO question post-data-through-json-webservices-in-iphone