Here is the webserice call
URL: http://mywedshare.com/wp-admin/admin-ajax.php?action=app_photoupload
gallery_ID,guestName, guestEmail, guestPasscode,photo_file (Multipart File Format)
I need to upload an UIImage. Im getting my NSData using the following line -
imageData = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage], 1);
and uploading it using
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
//Set Params
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
//Create boundary, it can be anything
NSString *boundary = #"------VohpleBoundary4QuqLuM1cE5lMwCy";
// 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];
//Populate a dictionary with all the regular values you would like to send.?action=app_photoupload?
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:#"app_photoupload" forKeyPath:#"action"];
[parameters setValue:galleryID forKey:#"gallery_ID"];
[parameters setValue:galleryName forKey:#"guestName"];
[parameters setValue:guestEmail forKey:#"guestEmail"];
[parameters setValue:guestPasscode forKey:#"guestPasscode"];
// add params (all params are strings)
for (NSString *param in parameters) {
[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", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSString *FileParamConstant = #"photo_file";
NSData *imageData2 = imageData;
//Assuming data is not nil we add this to the multipart form
if (imageData2)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"wedshare.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData2];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
// set URL
NSString *url = #"http://mywedshare.com/wp-admin/admin-ajax.php";
[request setURL:[NSURL URLWithString:url]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if ([httpResponse statusCode] == 200) {
[HUD showUIBlockingIndicatorWithText:#"Success"];
[HUD hideUIBlockingIndicator];
NSLog(#"success");
}
}];
Im getting Success but the image itself is not being uploaded onto the server. Please help me.
I'd suggest:
Run the app on the simulator and run Charles (or something like it) at the same time, and make sure the request looks ok. I don't see anything obviously wrong here from the client side. Still, Charles can tell us if the request looks ok.
I obviously cannot speak to the server side of it (e.g. the choice of the photo_file field name, the other properties, etc.). How confident are you regarding the API?
You are declaring success if the statusCode is 200. The 200 is a HTTP level status code that means is that the request was received and the server responded successfully, but it doesn't necessarily mean that the upload was successful. It looks like that URL returns some code, and you might want to examine that value before you start declaring success.
Related
I have a problem. I am making POST request to server. In which I am uploading an Image with user_id of User.I need to send user_id in x-www-form-urlencoded and Image in from-data. I tried many ways but every time user_id is undefined in server. How can I send both in same request.
Here is my CODE:
NSString *urlString = [[NSString alloc]initWithString:[NSString stringWithFormat:#"MY URL TO UPLOAD IMAGE "]];
urlString=[urlString stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:100];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"------V2ymHFg03ehbqgZCaKO6jy";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
[request addValue:#"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
NSDictionary *params = #{#"user_id":#"213"};
NSMutableData *body = [NSMutableData data];
for (NSString *param in params) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: x-www-form-urlencoded; 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(image, .3);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"my-file\"; filename=\"%#.jpg\"\r\n",#"213"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpg\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];
NSURLResponse *urlResponse;
NSError *error;
NSData * data=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
Output: data = nil
In order to send your data through POST
[request setHTTPMethod:#"POST"];
NSString *myString = [NSString stringWithFormat:#"value1=test3&value2=test"];
[request setHTTPBody:[myString dataUsingEncoding:NSUTF8StringEncoding]];
I'm trying to upload a multipart image to server where image is converted to NSData and this NSdata is sent a parameter in HTTP body in a JSON string. Unfortunately the app crashes with this message:
**'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteMutableData)'**
I understand that the problem is with JSON. The sample code:
-(void) uploadmultipartimage {
NSString * urlimageupload = [NSString stringWithFormat:#"%#api/mobile_profiles/avatar_upload",URLPrefixCertintell];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlimageupload]];
NSData *imageData = UIImageJPEGRepresentation(_profileimage, 1.0);
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"PUT"];
NSString *boundary = #"unique-consistent-string";
// 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", #"imageCaption"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", #"Some Caption"] dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=%#; filename=imageName.jpg\r\n", #"imageFormKey"] dataUsingEncoding:NSUTF8StringEncoding]];
[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]];
NSDictionary *jsonString = #{#"user":#{#"user_token":[[NSUserDefaults standardUserDefaults] stringForKey:#"user_token"]},#"api_key":APIKey,#"profile":body};
//***Code Crashes here**//
NSData *postData = [NSJSONSerialization dataWithJSONObject:jsonString options:0 error:nil];
//
// setting the body of the post to the reqeust
[request setHTTPBody:postData];
// 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, NSData *data, NSError *error) {
if(data.length > 0) {
//success
}
}];
}
You need to convert your data to NSString:
NSString *g=[[NSUserDefaults standardUserDefaults] stringForKey:#"user_token"];
if (!g)
g=#"Default Token";
NSString *base64Body = [body base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSDictionary *jsonString = #{#"user":#{#"user_token":g},#"api_key":APIKey,#"profile":base64Body};
Then you can always argue about the possibility of nil when reading the "user_token".
Take care,
/Anders.
I am new IOS developer and I am facing a headache issue regarding to use multipart for uploading file and some text, i have tried a lot on here
The status code always return 400. I tired to test my web service with another way, such as build Rest client by HttpComponent API, using RestEASY client and both of them worked successfully, how funny!
I am using Xcode 6.1.1, my source code:
-(void)uploadPhoto {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://localhost/my-rest-service"]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:#"avatar_temp"], 1.0);
NSString *boundary = #"12345-6789-abc"; //
// 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:#"%#--", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=%#\r\n\r\n", #"type"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", #"HELLO"] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if(data.length > 0){
//success
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"[String] %#", str);
}
}];
}
Any suggestions will help my investigation
Thanks
An
You forgot to add imageData so add:
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
A full working example:
[request setHTTPMethod:#"POST"];
NSData *imageData = UIImageJPEGRepresentation(imageToSave,0.9); //change imageToSave with your own UIImage defined
NSString *filenames = #"first field";
NSString *token = #"second field";
NSString *boundary = #"---------------------------14738723651466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
////+++ first form field for post -> for example a field called filenames see NSString above
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
////--- the second field for form post-> we named token see above NSString
////+++
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[token dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
////---
////+++ the file used to upload /post
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"ceva.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
////---
// setting the body of the post to the reqeust
[request setHTTPBody:body]; // now lets make the connection to the web
Now all the you want to make is check your php code on server side.
I am developing an iOS app and I am using node.js for server side scripting. I am facing problem in uploading image to server from iOS app. It is working fine if I am uploading image from webpage form. But if uploading from app side it is not working.
//test file
h3 Pic Upload
form(action='/pic_upload', method='post',enctype='multipart/form-data')
| user_pic:
input(type='file', name='user_pic')
input(type='submit')
//app.js
var userlogin =require('./routes/userlogin');
app.post('/pic_upload', userlogin.picUpload);
//userlogin.js
//picUpload function
exports.picUpload = function(req, res) {
console.log(req.files); // showing undefined, when called from IOS app
// pic upload script...
});
I have tried sending image from app side as data or file parameter but it did not work. How to send the file parameter from app side so that I can easily upload the image to server? Kindly suggest a way to tackle the problem.
This code works in my app. Try this.
NSData *imgData = UIImageJPEGRepresentation(newImg, 0.2);
NSString *str = #"displayImage";
NSString *urlString = #"http://some.url.com/post";
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithFormat:#"---------------------------14737809831464368775746641449"];
// 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--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"currentEventID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"52344457901000006" dataUsingEncoding:NSUTF8StringEncoding]];
// add image data
if (imgData) {
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"displayImage\"; filename=\"image.jpg\"\r\n"]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", str] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imgData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set URL
[request setURL:[NSURL URLWithString:urlString]];
NSString *bodyStr = [[NSString alloc]initWithData:body encoding:NSUTF8StringEncoding];
NSLog(#" %#",bodyStr);
NSURLConnection *lot_Connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
if(lot_Connection)
{
webdata = [[NSMutableData alloc] init];
}
In my app I have to perform a multipart POST request. In the request I have to send a file that is stored in the device's photo Library. After it is finished uploading and received the returned data, I send the request a release message, but Instruments shows me the data persists in the device's memory.
This is how I create the request object:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:YES];
[request setTimeoutInterval:30];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
// 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 alloc] init];
// add parts (all parts are strings) `parts` is a NSDictionary object
for(id key in parts) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parts objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// add files data - `file` is a ALAssetRepresentation object
NSData *fileData;
NSString *filename = [file filename];
Byte *buffer = (Byte*)malloc(file.size);
NSUInteger buffered = [file getBytes:buffer fromOffset:0.0 length:file.size error:nil];
fileData = [[NSData alloc] initWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
if (fileData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
if ([requestType isEqualToString:PUBLISH_TYPE_VC_MANDA]) {
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"attachment\"; filename=\"%#\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
} else {
[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:fileData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[fileData release];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
[body release];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"Keep-Alive" forHTTPHeaderField:#"Connection"];
// set URL `adress` is a NSURL object
[request setURL:address];
//set cookie
if (storedCookie) {
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:[storedCookie name], NSHTTPCookieName, myDomain, NSHTTPCookieDomain,#"/",NSHTTPCookiePath, [storedCookie value], NSHTTPCookieValue, nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:properties];
NSArray* cookies = [NSArray arrayWithObjects: cookie, nil];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookies];
[request setAllHTTPHeaderFields:headers];
}
After this, and after I received the response, i perform [request release] but the allocated bytes stay in memory.
I ran Instruments in the Allocations mode, and it is claiming that there is an object of category Malloc 91MB (the size of the ALAsset I'm uploading). By looking at the Call tree, the Symbol that's taking up all the memory is [NSConcreteMutableData appendBytes:length:] inside the method described above.
Why isn't it freeing the bytes ? And more importantly, why does it have to move the data to the device's memory ? I've seen apps like Facebook upload very large videos from the gallery without any memory problems.
Thank you very much for your attention, any help or directions would be greatly appreciated
EDIT: Further investigation showed me that when I use [request release] the request object is released, but it's body isn't. Pretty weird behaviour...
EDIT2: I explicitly used [request.HTTPBody release]; and the bytes were released. I still cannot believe that [request release] wasn't freeing it's body. Could this be a bug ?
Your request has a retainCount of 2:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; // 1
...
[request retain]; // 2
Then if you do [request release], it's decreased by one.
The solution is not to do [request retain].
After further investigating, the memory wasn't being released because the request's property HTTPBody needed to manually be released (see my second edit).
As to sending large chunks of data in a NSRequest, there is an easier way that won't consume the device's memory. You have to write the file to the disk, and then map it. I have described it in details in this SO answer. Hope it helps someone