Point :
I am working on uploading image using Amazon Pre-Signed URL in my iOS App
Now the issue is i am getting status code 400 everytime when i upload it
It should give 204 status code if it works well
Pre-Signed URL works well while testing on Postman but not working on App side.
Here is the implementation :
- (void)doUploadURL {
UIImage *myimage = [UIImage imageNamed:#"testimage.png"];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSString *boundary = #"---011000010111000001101001";
NSMutableData *body = [NSMutableData data];
// AcL parameter
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"acl\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAcL dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Content-Type parameter
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"Content-Type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strContentType dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// key parameter
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strKey dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// x-amz-meta-filename parameter
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"x-amz-meta-filename\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strMetaFilename dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Credential parameter
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"X-Amz-Credential\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strCredential dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Algorithm parameter
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"X-Amz-Algorithm\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAlgorithm dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Date parameter
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"X-Amz-Date\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strAmzDate dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Policy parameter
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"Policy\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strPolicy dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// X-Amz-Signature parameter
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"X-Amz-Signature\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[strSignature dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// file
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: attachment; name=\"file\"; filename=\"testimage.png\"\\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *headers = #{ #"content-type": #"multipart/form-data"};
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://visual-showcase.s3-ap-southeast-2.amazonaws.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3600.0];
[request setHTTPMethod:#"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:body];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(#" Final Response : %d",httpResponse.statusCode);
if (error) {
NSLog(#"Error : %#", error);
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Failed to upload" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil] show];
return;
});
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *err = nil;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
}];
[dataTask resume];
}
Response code :
<NSHTTPURLResponse: 0x7d10e2c0> { status code: 400, headers {
Connection = close;
"Content-Type" = "application/xml";
Date = "Thu, 02 Feb 2017 07:20:18 GMT";
Server = AmazonS3;
"Transfer-Encoding" = Identity;
"x-amz-id-2" = "HIFhwBaj/ejjGoCgD2vacG2AtoxEFDXxSr6eulZtzUbuLoTcLT+AA9k6093Jyv5OIuK2wpi7+9c=";
"x-amz-request-id" = C221CD8DA2D80BCD;
} }
Question :
My question is, is there anything we need to set while uploading files using Amazon Pre-Signed URL ? Or we can just implement it like normal API calls ?
As per my knowledge & it should be implemented similar way like normal APIs
Please do let me get any hint or if something wrong i am doing in request format
Thanks in Advance.
Finally resolved the issue by changing the request parameters format. Below is the working code.
- (void)doUploadURL {
UIImage *myimage = [UIImage imageNamed:#"testimage.png"];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSString *boundary = #"------VohpleBoundary4QuqLuM1cE5lMwCy";
NSMutableData *body = [NSMutableData data];
//Populate a dictionary with all the regular values you would like to send.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:strAcL forKey:#"acl"];
[parameters setValue:strContentType forKey:#"Content-Type"];
[parameters setValue:strKey forKey:#"key"];
[parameters setValue:strMetaFilename forKey:#"x-amz-meta-filename"];
[parameters setValue:strCredential forKey:#"X-Amz-Credential"];
[parameters setValue:strAlgorithm forKey:#"X-Amz-Algorithm"];
[parameters setValue:strAmzDate forKey:#"X-Amz-Date"];
[parameters setValue:strPolicy forKey:#"Policy"];
[parameters setValue:strSignature forKey:#"X-Amz-Signature"];
// 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]];
}
// Upload image
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"file\"; filename=\"testimage.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type:image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"APIURL"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3600.0];
[request setHTTPMethod:#"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
[request setHTTPBody:body];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
if (error) {
NSLog(#"Error : %#", error);
dispatch_async(dispatch_get_main_queue(), ^{
[[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Failed to upload" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil] show];
return;
});
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSError *err = nil;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
}
}];
[dataTask resume];
}
Hope it will help guiding someone.
Related
I am using this code and able to send only single image to the server. I
want to send multiple selected image files to the server.
Please look this code for possible mistake and Please help me
out to solve this issue. Thank you for your time.
-(void)submitDataToServer
{
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
NSNumber *randomNo = [NSNumber numberWithInteger: timeStamp];
NSLog(#"Random No=%#",randomNo);
NSLog(#"Project ID=%#",projectId);
NSLog(#"DATE=%#",dateString);
NSLog(#"USER ID=%#",userID);
NSLog(#"COMMENT =%#",projectNoteTextView.text);
NSLog(#"TASK ID=%#",task_Id);
//-- Convert string into URL
NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:UPLOADDATA_URL]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
//-- Append data into posr url using following method
NSMutableData *body = [NSMutableData data];
//Random No
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"random"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",randomNo] dataUsingEncoding:NSUTF8StringEncoding]];
//Project ID
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"project_id"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",projectId] dataUsingEncoding:NSUTF8StringEncoding]];
//Date
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"date"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",dateString] dataUsingEncoding:NSUTF8StringEncoding]];
//User ID
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"user_id"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",userID] dataUsingEncoding:NSUTF8StringEncoding]];
//Comment
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"comment"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",projectNoteTextView.text] dataUsingEncoding:NSUTF8StringEncoding]];
//Task ID
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"task_assign_id"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",task_Id] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"ARRAYCOUNT: %lu", (unsigned long)arrImage.count);
for (int y = 0; y < [arrImage count]; y++)
{
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString * image_name =[NSString stringWithFormat:#"%#%d",#"image",y];
NSLog(#"IMAGE NAME:%#", image_name); //self.arrImage
UIImage* imageFile = [arrImage objectAtIndex:y];
CGSize newSize = CGSizeMake(500.0f, 500.0f);
UIGraphicsBeginImageContext(newSize);
[imageFile drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *dataImg = [NSData
dataWithData:UIImagePNGRepresentation(newImage)];
//NSData *dataImg = [NSData dataWithData:UIImagePNGRepresentation([arrImage objectAtIndex:y])];
NSLog(#"added %i", y+1);
NSLog(#"Images, send to server:%#", dataImg);
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"site_image\"; filename=\"%#\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataImg]];
}
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//-- Sending data into server through URL
[request setHTTPBody:body];
//-- Getting response form server
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//-- JSON Parsing with response data
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(#"Result = %#",result);
}
Use this code in for loop for multiple images.
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:#"imgProfile"];
NSString *str=[NSString stringWithFormat:#"%#",yourUrl];
NSString *urlString = [NSString stringWithFormat:#"%#",str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"profilePicture\"; filename=\"ved.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter image
if (imageData.length<1) {
[body appendData:[#" " dataUsingEncoding:NSUTF8StringEncoding]];
}
else {
[body appendData:[NSData dataWithData:imageData]];
}
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// 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:[#"profileUpdate" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter userID
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"userid"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//userId , email , name , tockenId , deviceType , cCode,profilePicture,phone
//parameter email
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[txtEmail.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter name
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"name\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",txtName.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter tockenId
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"tockenId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"Device_Token"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// Parameter deviceType
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"deviceType\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",#"iOS"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter cCode
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"cCode\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[txtCountryCode.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// //parameter profilePicture
// [body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"profilePicture\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//
// [body appendData:[#"ved" dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter phone
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"phone\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[txtPhone.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
NSLog(#"Update profile Image %#",dict);
dispatch_async(dispatch_get_main_queue(), ^{
if ([[dict valueForKey:#"result"] isEqualToString:#"sucess"]) {
[WebServiceHelper showAlertViewForTitle:#"Message" andMessage:[dict valueForKey:#"message"]];
}
else{
[WebServiceHelper showAlertViewForTitle:#"Updating Failed!" andMessage:[dict valueForKey:#"message"]];
}
});
});
}
After recheck I got that there was I was passing wrong image Array. Now this is working after changed the same. Thanks you all for your time and consideration.
NSLog(#"ARRAYCOUNT: %lu", (unsigned long)imgArray.count);
for (int y = 0; y < [arrImage count]; y++)
{
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString * image_name =[NSString stringWithFormat:#"%#%d",#"image",y];
NSLog(#"IMAGE NAME:%#", image_name);
UIImage* imageFile = [arrImage objectAtIndex:y];
CGSize newSize = CGSizeMake(500.0f, 500.0f);
UIGraphicsBeginImageContext(newSize);
[imageFile drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *dataImg = [NSData
dataWithData:UIImagePNGRepresentation(newImage)];
//NSData *dataImg = [NSData dataWithData:UIImagePNGRepresentation([imgArray objectAtIndex:y])];
NSLog(#"added %i", y+1);
NSLog(#"Images, send to server:%#", dataImg);
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"site_image\"; filename=\"%#\"\r\n",image_name] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:dataImg]];
Blockquote
}
NSString *urlString = #"http://example.com/avatar";
NSLog(#"%#",urlString);
NSString *imageName = #"hello";
NSString *string = [NSString stringWithFormat:#"%#%#%#", #"Content-Disposition: form-data; name=\"avatar\"; filename=\"", imageName, #".jpg\"\r\n\""];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449MJ";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data"];
[request addValue:contentType forHTTPHeaderField: #"Content-type"];
NSString *contentType1 = [NSString stringWithFormat:#"%#",customer_api_key];
[request addValue:contentType1 forHTTPHeaderField:#"Authorization"];
NSString *contentType2 = [NSString stringWithFormat:#"app-customer"];
[request addValue:contentType2 forHTTPHeaderField:#"Requester"];
UIImage *image=DPView.image;
NSData *imageData =UIImageJPEGRepresentation(image, 1);
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:string] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"avatar_name\"\r\n\r\n%#", NameTF.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"avatar_mime_type\"\r\n\r\nimage/jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"customer_id\"\r\n\r\n%d",customer_id] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSLog(#"Body = %#",body);
NSString *bodydecoded = [[NSString alloc] initWithData:body encoding:NSASCIIStringEncoding];
NSLog(#"Bodydecoded = %#",bodydecoded);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(#"ReturnData = %#",returnData);
if (returnData) {
NSString *s11= [[NSString alloc] initWithData:returnData encoding:NSISOLatin1StringEncoding];
// NSDictionary *responseDictionary1= (NSDictionary *)s11;
NSLog(#"S11 = %#",s11);
NSDictionary *JSON =
[NSJSONSerialization JSONObjectWithData: [s11 dataUsingEncoding:NSASCIIStringEncoding]
options: NSJSONReadingMutableContainers
error: nil];
NSLog(#"json finally= %#",JSON);
hello everyone, I am new to IOS so please spare me.
Here I am trying to send Image with 3 header for authorization and 3 contents in body for image related info. But as I append the imagedata and then decode the body, I get this output :
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar"; filename="hello.jpg"
"Content-Type: image/jpeg
ÿØÿà
rest of the content after image is not created or what?
but when do not append the imagedata, i get the whole form-data created normally as :
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar"; filename="hello.jpg"
"Content-Type: image/jpeg
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar_name"
fgh
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="avatar_mime_type"
image/jpg
-----------------------------14737809831466499882746641449MJ
Content-Disposition: form-data; name="customer_id"
xyz
-----------------------------14737809831466499882746641449MJ
and when i pass this data(in both cases), i get invalid data error from server.(obviously)
please help me, I am stuck.
also the headers I am passing is without boundary = %#,boundary, is that correct. I removed it because I was getting invalid api key error from server.(off-course the authorization error)
Thanks in advance.
please use this for creating multipart form requset
NSString *boundary = [NSString stringWithFormat:#"Boundary-%#", [[NSUUID UUID] UUIDString]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
don't add header in body add as follows
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
create body ad follows
NSMutableData *body = [NSMutableData data];
[dicParams enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
}];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"image\"; filename=\"%#\"\r\n", strName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: %#\r\n\r\n", strMime] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:dataImage];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
request is created and use as you want
===================
when you are adding parameters you are doing like following
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"avatar_name\"\r\n\r\n%#", NameTF.text] dataUsingEncoding:NSUTF8StringEncoding]];
"\r\n" is the end tag form which one field is completed.
your first parameter will start with #"\r\n--%#\r\n" which is wrong. this means one field ended and another field is started instead of that you have to do as mentioned below.
add boundary like this
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
add field name like this
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", parameterKey] dataUsingEncoding:NSUTF8StringEncoding]];
add field value like this
[body appendData:[[NSString stringWithFormat:#"%#\r\n", parameterValue] dataUsingEncoding:NSUTF8StringEncoding]];
you will get success.
I would like to upload 1pdf file and array of images.
I can upload 1pdf and 2jpg files, if jpg files more than two I get message: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No value.) UserInfo=0x174268000 {NSDebugDescription=No value.}
Why is this happening? Also I tested server with POSTMAN and it work well.
NSURL *URL = [NSURL URLWithString:constFileUploadURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"0xLhTaLbOkNdArZ";
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", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *userid = [NSString stringWithFormat:#"%li",userID];
[body appendData:[userid dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[constBackendKey dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSData *data in arrayWithFiles)
{
NSString *fileName;
if ([arrayWithFiles indexOfObject:data] == 0)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
fileName = #"Content-Disposition: form-data; name=\"files\"; filename=\"PdfFile.pdf\"\r\n";
[body appendData:[fileName dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
else
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"files%ld\"; filename=\"image%ld.jpg\"\r\n",[arrayWithFiles indexOfObject:data],[arrayWithFiles indexOfObject:data]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
_connection = [NSURLConnection connectionWithRequest:request delegate:self];
Cod works, problem was on server side. File uploading 50mb, a post size 8mb.
I am using update_with_media Rest API to post a picture to twitter. I am getting HHTP 410 error everytime when I hit the API
My code snippet is : In SA_OAuthTwitterEngine.m
- (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType
{
NSString *boundary = #"----------------------------991990ee82f7";
NSURL *finalURL = [NSURL URLWithString:#"https://upload.twitter.com/1.1/statuses/update_with_media.json"];
if (!finalURL) {
return nil;
}
OAMutableURLRequest *theRequest = [[[OAMutableURLRequest alloc] initWithURL:finalURL
consumer:self.consumer
token:_accessToken
realm: nil
signatureProvider:nil] autorelease];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPShouldHandleCookies:NO];
// Set headers for client information, for tracking purposes at Twitter.
[theRequest setValue:_clientName forHTTPHeaderField:#"X-Twitter-Client"];
[theRequest setValue:_clientVersion forHTTPHeaderField:#"X-Twitter-Client-Version"];
[theRequest setValue:_clientURL forHTTPHeaderField:#"X-Twitter-Client-URL"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[theRequest setValue:contentType forHTTPHeaderField:#"content-type"];
NSMutableData *body = [NSMutableData dataWithLength:0];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"media[]\"; filename=\"1.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Honeymoon uploads image\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// --------------------------------------------------------------------------------
// modificaiton from the base clase
// our version "prepares" the oauth url request
// --------------------------------------------------------------------------------
[theRequest prepare];
[theRequest setHTTPBody:body];
// Create a connection using this request, with the default timeout and caching policy,
// and appropriate Twitter request and response types for parsing and error reporting.
MGTwitterHTTPURLConnection *connection;
connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest
delegate:self
requestType:requestType
responseType:responseType];
if (!connection) {
return nil;
} else {
[_connections setObject:connection forKey:[connection identifier]];
[connection release];
}
return [connection identifier];
}
In my MGTwitterEngine.m
- (NSString *)upploadd:(UIImage *)img status:(NSString *)status
{
return [self _uploadImage:img requestType:MGTwitterImageRequest responseType:(MGTwitterStatus)];
}
Please help me to find out the error. I am really confused.
With API v1.1, use api.twitter.com as the domain instead of upload.twitter.com.Try using this url
https://api.twitter.com/1.1/statuses/update_with_media.json
for the status share just replace the below code
[body appendData:[[NSString stringWithFormat:#"--%#\r\n\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",status] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
with the following code
//Status
[body appendData:[[NSString stringWithFormat:#"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[statusStr dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
Over the past week I have been trying to get this to work. What I am trying to do is upload an image to an S3 service as part of the pretty hacky way that this API that I am working with uses to upload images. The API is documented here: http://www.thingiverse.com/developers/upload-guide . I am having problems with step 2. They want you do upload to S3, then tell their other servers that the file is uploaded to S3, rather than doing it for you. Regardless, this is the code that I am using to upload the image:
+ (NSString *)uploadPictureWithParameters:(NSDictionary *)parameters toURL:(NSString *)actionURL withImage:(UIImage *)sourceImage {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:#"POST"];
NSString *boundaryConstant = #"-------------------------acebdf13572468";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundaryConstant];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
NSLog(#"%#", [parameters allKeys]);
if ([parameters objectForKey:#"AWSAccessKeyId"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"AWSAccessKeyId\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"AWSAccessKeyId"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"bucket"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"bucket\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"bucket"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"key"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"key\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"key"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"acl"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"acl\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"acl"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"success_action_redirect"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"success_action_redirect\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"success_action_redirect"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"policy"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"policy\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"policy"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"signature"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"signature\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"signature"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"Content-Type"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"Content-Type\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:#"Content-Type"]] dataUsingEncoding:NSUTF8StringEncoding]];
}
if ([parameters objectForKey:#"Content-Disposition"]) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"Content-Disposition\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-disposition: attachment; filename=photo.png\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
// This would put them in the wrong order:
/*for (NSString *param in [parameters allKeys]) {
[body appendData:[[NSString stringWithFormat:#"---------------------------%#\r\n", boundaryConstant] 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]];
}*/
NSLog(#"%d", body.length);
UIImage *resizedImage = [sourceImage resizedImageToFitInSize:CGSizeMake(700, 700) scaleIfSmaller:NO];
NSData *imageData = UIImagePNGRepresentation(resizedImage);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"photo.png\"\r\n", #"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *requestBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(#"Request Body: %#", requestBody);
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%d", body.length);
NSString *requestBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(#"Request Body After Image: %#", requestBody);
[request setHTTPBody:body];
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
NSString * urlString = [NSString stringWithFormat:#"%#", actionURL];
NSLog(#"%#", urlString);
NSURL *aUrl = [NSURL URLWithString: urlString];
[request setURL:aUrl];
NSError * error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &error];
NSLog(#"%p, %#", error, error.localizedDescription);
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
//NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
return responseString;
}
The error that I am getting from that is this:
Invalid according to Policy: Policy Condition failed: ["eq", "$Content-Disposition", ""]
I've been researching this online and it seems that the errors produced by S3 are typically not of much relevance to the error. So, can anyone tell me what I am doing incorrectly here?
Thanks