Search code examples
iosimageposttumblrurl-encoding

Binary Image POST - iOS


I have an app which makes a OAuth 1.0a POST request to the Tumblr API. I have been following their documentation for how to upload a photo and although I have set the parameters, it doesn't work.

The Tumblr API Documentation states that you need these in your parameters:

source - (string) - The photo source URL

data - (Array URL-encoded binary contents) - One or more image files (submit multiple times to create a slide show)

So I have setup my POST request as follows but I can't seem to get it to work. Here is my code:

// Tumblr POST Request.
NSString *inputted_text = [inputted_text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSData* imageData = UIImagePNGRepresentation(image_selected);

NSString *path = [NSString stringWithFormat:TUMBL_POST, tumblr_profilename];

NSDictionary *parameters;

if (media_post_check == 0) {
            // Text post.

            parameters = @{@"api_key" : TUMBL_CONSUMER_KEY,
                           @"type" : @"text",
                           @"body" : inputted_text};
        }

        else if (media_post_check == 1) {
            // Photo post.

            NSData *data_64 = [imageData base64EncodedDataWithOptions:0];

            parameters = @{@"api_key" : TUMBL_CONSUMER_KEY,
                           @"type" : @"photo",
                           @"caption" : inputted_text,
                           @"data" : @[data_64]};
        }

        else if (media_post_check == 2) {
            // Video post.

            parameters = @{@"api_key" : TUMBL_CONSUMER_KEY,
                           @"type" : @"link",
                           @"title" : @"Hello there",
                           @"url" : pub_video_url};
        }

        // Build authorized request based on path, parameters, tokens, timestamp etc.
        NSURLRequest *preparedRequest = [OAuth1Controller preparedRequestForPath:path parameters:parameters HTTPmethod:@"POST" oauthToken:token_tumblr oauthSecret:token_secret_tumblr];

        NSData *urlData;
        NSURLResponse *response;
        NSError *error;

        // Make synchronous request.
        urlData = [NSURLConnection sendSynchronousRequest:preparedRequest returningResponse:&response error:&error];
        NSLog(@"%@", response);

Do I need to send the image data or a link to it? What am I doing wrong?

UPDATE 1

I am now getting this error response:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x112a50a90 {NSUnderlyingError=0x10d6e1370 "The network connection was lost.", NSErrorFailingURLStringKey=http://api.tumblr.com/v2/blog/supertecnoboff.tumblr.com/post?api_key=wPqSZcRrJM4Rxu9oRECjcAzcvkq8EPwYq2LN98W13U39tDRPbZ&caption=Test%2520star&data%5B%5D=%3C6956424f%20527730206a........data...........14d6a%2048686871%204876672f%202f394763%2045684a68%205a574d41%2041414141%205355564f%20524b3543%205949493d%3E&type=photo, NSLocalizedDescription=The network connection was lost.}

Thanks for your time, Dan.


Solution

  • I didn't manage to solve the problem myself. But after a lot of searching I came across this StackOverflow post of a developer who made a great Tumblr Uploader Class for iOS. So helpful and works perfectly. If you are interested, here it is: https://stackoverflow.com/a/7431731/1598906