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

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.

Related

Parse REST API uploading binary image data in 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];

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;

send multiple data to server

I managed to send some string with image data to the server. However array which contains the images has more than 1 object. I can perfectly get the first image from server but rest seems to disappear. Is there any way to send multiple data files to server or is there anything wrong in my for loop to send the images ? Server side code seems to work fine since it gets the strings and the first image file so I think there must be something wrong in the objective-c part of my code.
NSString *urlString = #"http://www.somesite.com/cgi-bin/somefile.py";
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 *body = [NSMutableData data];
NSString *filenames = [NSString stringWithFormat:#"some string"]; //set name here
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"sessionString\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userEmail\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[sharedSingletonCenter.emailString dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
for (int i = 0; i < [self.pagesArray count]; i++) {
NSData* imageData = UIImagePNGRepresentation(self.pagesArray[i]);
NSLog(#"added %i", i+1);
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file_%i\"; filename=\".png\"\r\n", i + 1] 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];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#", returnString);
NSLog(#"finish");
The problem is that multipart form elements are separated with
[NSString stringWithFormat:#"\r\n--%#\r\n",boundary]
But then terminated with
[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary]
But you're inserting that termination boundary string at the end of each file. You want to change your code to do that only at the end. Thus:
NSMutableData *body = [NSMutableData data];
NSString *filenames = [NSString stringWithFormat:#"some string"]; //set name here
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"sessionString\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userEmail\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[sharedSingletonCenter.emailString dataUsingEncoding:NSUTF8StringEncoding]];
for (int i = 0; i < [self.pagesArray count]; i++) {
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSData* imageData = UIImagePNGRepresentation(self.pagesArray[i]);
NSLog(#"added %i", i+1);
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file_%i\"; filename=\".png\"\r\n", i + 1] 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];

Best practice when using similar code in methods

I have a Utility method to comunicate with an API, it communicates using a POST HTTP request, in my utility-class i have a method called:
(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters;
wich takes the parameters and sets up the body of the POST. however, in one particular case i want to upload some images and the code is slightly altered to make uploading of said images possible, what is the best practice for this case? should i rename the method to:
(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;
and set nil as a parameter in all other cases, or should i set a bool in the method calling "makeConnectionWithParameters" and check in the method if the bool is set and in that case process the images?
Any other ideas to make the code prettier?
here is the method:
(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:BASE_URL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20];
request.HTTPMethod = #"POST";
NSString *boundary = #"myR4ND0Mboundary";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
//Lägg till inloggningsuppgifter för API-anropet
[parameters setValue:API_LOGIN forKey:#"api-login"];
[parameters setValue:API_PASSWORD forKey:#"api-password"];
//Lägg till alla parameterar i POST-bodyn
NSMutableData *body = [NSMutableData data];
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]];
}
if(hasImages)
{
int c = 0;
for(UIImage* image in self.images)
{
c++;
NSData *imageData = UIImageJPEGRepresentation(image, 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", [NSString stringWithFormat:#"image%d", c]] 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]];
}
imageData = nil;
}
}
//Sätt content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:body];
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(connection)
{
receivedData = [NSMutableData dataWithLength:0];
}
}
I hope this all makes sense. :)
Simply create two methods that make the request objects, not the actual connections:
(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters;
(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;
Inside the first method call:
[self makeConnectionWithParameters:parameters andImages:nil];
...and have a nil check for the images inside the second method. Use those two methods to get your request then make the NSURLConnection with the returned object.

Failing to POST NSData of PDF to server

I have an iPad application in which I need to send a server a PDF file, alongside two POST variables, a user ID and a job ID. I am using the code below to do this:
NSData *pdfData = [NSData dataWithContentsOfFile:currentPDFFileName];
NSData *validPDF = [[NSString stringWithString:#"%PDF"] dataUsingEncoding: NSASCIIStringEncoding];
if (!(pdfData && [[pdfData subdataWithRange:NSMakeRange(0, 4)] isEqualToData:validPDF]))
{
NSLog (#"Not valid");
}
NSLog (#"%#", currentPDFFileName);
NSLog (#"%#", [currentPDFFileName lastPathComponent]);
//create the body
NSMutableData *body = [NSMutableData data];
//create the POST vars
NSString *jobID = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"jobid\"\r\n\r\n%#", job.jobID];
NSString *userID = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userid\"\r\n\r\n%#", delegate.userID];
NSString *pdf = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"image_file\"; filename=\"%#\"\r\n", [currentPDFFileName lastPathComponent]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[jobID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[userID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[pdf dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/pdf\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:pdfData];
NSMutableURLRequest *pdfRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com"]];
[pdfRequest setValue:#"multipart/form-data; boundary=----foo" forHTTPHeaderField:#"Content-type"];
[pdfRequest setHTTPMethod:#"POST"];
[pdfRequest setHTTPBody:body];
NSData *pdfDataReply;
NSURLResponse *pdfResponse;
NSError *pdfError;
pdfDataReply = [NSURLConnection sendSynchronousRequest:pdfRequest returningResponse:&pdfResponse error:&pdfError];
NSString *pdfResult = [[NSString alloc] initWithData:pdfDataReply encoding:NSASCIIStringEncoding];
NSLog (#"%#", pdfResult);
The people on the other end of the connection are saying that they are not receiving the PDF file. I have tried all the usual suspects such as trying to find any nil objects.
Would greatly appreciate if anyone could please lend a hand with this!
Thanks.
Ricky.
Turns out there was an issue with the boundaries. I needed to add one at the end, after pdfData was added. Thank you for your help.