i develop program which searches for similar images(Reverse image search)
My Code:
Y_request = [[NSMutableURLRequest alloc]init];
[Y_request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[Y_request setHTTPShouldHandleCookies:NO];
[Y_request setTimeoutInterval:30];
[Y_request setHTTPMethod:#"POST"];
NSString *Y_boundary=#"myBoucndary";
NSString *Y_FileParamConstant=#"upfile";
Y_requestURL = [[NSURL alloc]initWithString:#"https://www.yandex.com/images/search"];
NSString *Y_contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",Y_boundary];
[Y_request setValue:Y_contentType forHTTPHeaderField:#"Content-Type"];
Y_body = [NSMutableData data];
NSDictionary *Y_params=#{
#"format":#"",
#"request":#"[{\"block\":\"b-page_type_search-by-image__link\"}]",
#"rpt":#"imageview"
};
for (NSString *Y_param in Y_params) {
[Y_body appendData:[[NSString stringWithFormat:#"--%#\r\n",Y_boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[Y_body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", Y_param] dataUsingEncoding:NSUTF8StringEncoding]];
[Y_body appendData:[[NSString stringWithFormat:#"%#\r\n", [Y_params objectForKey:Y_param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// add image data
NSData *Y_imageData = UIImageJPEGRepresentation(imageSearch, 0.1);
if (Y_imageData) {
[Y_body appendData:[[NSString stringWithFormat:#"--%#\r\n", Y_boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[Y_body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"image\"; filename=\"image.jpeg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[Y_body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[Y_body appendData: Y_imageData];
[Y_body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[Y_body appendData:[[NSString stringWithFormat:#"--%#--\r\n", Y_boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[Y_request setHTTPBody:Y_body];
[Y_request setURL:Y_requestURL];
[_myWebView loadRequest:Y_request];
This Yandex Search query. This query works only on the iPhone but does not work for Ipad,Why?.
Thanks for the answer.
Related
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.
The below is my method to upload a photo to the server. There are two parameters: DeviceToken and ContentModel.ContentModel that contains a dictionary. Inside 2 field there is AlbumID and ContentPath.
NSURL *rtfUrl = [[NSBundle mainBundle] URLForResource:#"AlertBody" withExtension:#".png"];
NSString *deviceToken = #"5FF2C5A6-3930-4102-99A7-A55107B4375C";
NSString *loStr = [NSString stringWithFormat:myUrl];
loStr = [loStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:loStr]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"deviceToken"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:deviceToken] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
NSDictionary *contentModel = #{
#"AlbumID" : #"0",
#"contentPath" : [rtfUrl absoluteString],
};
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",#"contentModel"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"%#",contentModel] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Adding Image
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:#"AlertBody.png"], 0.3);
[postbody appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"myfile\"; filename=\"%#\"\r\n",#"AlertBody.png"] dataUsingEncoding:NSUTF8StringEncoding]]; // filetype=\"image/png\";
[postbody appendData:[#"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Close form
[postbody appendData:[[NSString stringWithFormat:#"--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//send data to server
[request setHTTPBody:postbody];
NSString *msgLength = [NSString stringWithFormat:#"%d", [postbody length]];
[request addValue: msgLength forHTTPHeaderField:#"Content-Length"];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[NSString alloc] initWithData:responseData1 encoding:NSUTF8StringEncoding];
NSLog(#"response string ==%#;",responseString);
This is my code. What is the error in this? I am getting this response: Source code **500** .web service is created using ASP.net.
Please Help me. I am facing this problem a lot.
Can you try below code? It's tested and working fine. Hope it may help you to resolve your issue too.
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[postData appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"yourImage.jpg\"\r\n", #"file"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:imageData];
[postData appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
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