damog / www-tumblr

Perl interface for the Tumblr API
https://metacpan.org/module/WWW::Tumblr
13 stars 8 forks source link

video and audio posts failed when using data field #8

Closed xibios closed 10 years ago

xibios commented 10 years ago

First thanks for writing this perl package, it's very useful.

I'm testing the various types of Tumblr posts (http://www.tumblr.com/docs/en/api/v2#posting) and the post of type video and audio failed when I pass an array in the data field. Tumblr responds with an error: Error uploading video file. (code:400)

In their documentation, the data field is not supposed to be an array like for a photo post.

So I tried by changing your code, in Tumblr.pm function _oauth_request from:

    $message = POST('http://api.tumblr.com/v2/' . $url_path,
        Content_Type => 'form-data',
        Authorization => $authorization_signature,
        Content => [
            %$params, ( $data ? do {
                my $i = -1;
                map { $i++; 'data[' . $i .']' => [ $_ ] } @$data
            } : () )
        ]);

to

$message = POST('http://api.tumblr.com/v2/' . $url_path,
        Content_Type => 'form-data',
        Authorization => $authorization_signature,
        Content => [
                %$params, ( $data ? do {
                        if (ref($data) eq 'ARRAY') {
                            my $i = -1;
                            map { $i++; 'data[' . $i .']' => [ $_ ] } @$data
                        }
                        else
                        {
                            'data' => [ $data ]
                        }
                } : () )
        ]);

and now when using such blog post method:

my $post = $blog->post( type => 'video', data => '/home/david/larry.mp4' , # not an array here, just a literal string caption => 'Larry David video' );

it works fine. Same for audio post.

If you agree with my fix, please integrate it in your official CPAN package.

Warm regards,

Emmanuel

damog commented 10 years ago

Hi, nice catch. I suppose I never really tried to upload videos or audio clips :)

Would you be able to provide an actual pull request? That's much easier :)

damog commented 10 years ago

It had already been merged.