6

I'm trying to upload a picture to ImageShack using their API:

- (void)uploadImage2:(UIImage *)image
{
    NSData *imageToUpload = UIImagePNGRepresentation(image);

    if (imageToUpload)
    {
        NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
        [parameters setObject:@"XXXX" forKey:@"key"];
        [parameters setObject:@"json" forKey:@"format"];
        //[parameters setObject:@"application/json" forKey:@"Content-Type"];

        AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://post.imageshack.us"]];

        NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
            [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"logo.png" mimeType:@"image/png"];
        }];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
             NSLog(@"response: %@",jsons);

         }
                                         failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             if([operation.response statusCode] == 403)
             {
                 //NSLog(@"Upload Failed");
                 return;
             }
             //NSLog(@"error: %@", [operation error]);

         }];

        [operation start];
    }
}

As a response I get error message back with no error explanations:

{
    "error_code" = "upload_failed";
    "error_message" = "Upload failed";
    status = 0;
}

Can anyone help me with that? What is the proper way to do it?

Oleg
  • 2,984
  • 8
  • 43
  • 71

3 Answers3

3

Okay, I have managed to solve my problem. All parameters have to be set in form body, not as request values. It looks quite simple:

 NSData *imageToUpload = UIImagePNGRepresentation([UIImage imageNamed:@"logo.png"]);


    if (imageToUpload)
    {
        NSString *urlString = @"https://post.imageshack.us";

        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlString]];
        NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
            [formData appendPartWithFileData:imageToUpload name:@"fileupload" fileName:@"image" mimeType:@"image/png"];
            [formData appendPartWithFormData:[@"XXXXXX" dataUsingEncoding:NSUTF8StringEncoding] name:@"key"];
            [formData appendPartWithFormData:[@"json" dataUsingEncoding:NSUTF8StringEncoding] name:@"format"];
        }];



        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
             NSLog(@"response: %@",jsons);

         }
                                         failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             if([operation.response statusCode] == 403)
             {
                 NSLog(@"Upload Failed");
                 return;
             }
             NSLog(@"error: %@", [operation error]);

         }];

        [httpClient enqueueHTTPRequestOperation:operation];
    }}

Hope this will help someone!

Oleg
  • 2,984
  • 8
  • 43
  • 71
2

Try this and let us know if it works :

      NSData *imageToUpload = UIImageJPEGRepresentation(uploadedImgView.image,1.0);//(uploadedImgView.image);
      if (imageToUpload)
      {
        NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
        [parameters setObject:@"MY API KEY" forKey:@"key"];
        [parameters setObject:@"json" forKey:@"format"];

        AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"https://post.imageshack.us"]];

        NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload_api.php" parameters:parameters constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
            [formData appendPartWithFileData: imageToUpload name:@"image" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
        }];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
         {
             NSDictionary *jsons = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
             //NSLog(@"response: %@",jsons);

         }
                                         failure:^(AFHTTPRequestOperation *operation, NSError *error)
         {
             if([operation.response statusCode] == 403)
             {
                 //NSLog(@"Upload Failed");
                 return;
             }
             //NSLog(@"error: %@", [operation error]);

         }];

        [operation start];
    }
Sharon Nathaniel
  • 1,467
  • 20
  • 28
1

Uploading an Image ( Basic )

 Endpoint : https://post.imageshack.us/upload_api.php Parameters :
* key : The api key for your application; found in email sent after filling out our API Key Request form
* fileupload : image file
* format : json tags : a CSV list of tags to describe your picture public : A string setting your image to public or private where "yes" is public and "no" is private. Images are public by default.

Did you use key request and get your own key for the upload process?

Obtaining an API Key
To obtain an API key, please use our API Key Request form.

Also set format application/json

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • Yes, I use the key. It showed me an error that I should do it when I didn't use it, so I'm sure the problem is not related to the API key – Oleg May 30 '13 at 09:54