向Perl上传

时间:2015-12-03 20:39:47

标签: perl file-upload box

在尝试关注this answer上传使用Perl的LWP到的API时,我遇到了一个问题。 Box希望收到parent参数to upload a file,我无法说服Perl传递。

# setup LWP
my $ua = LWP::UserAgent->new;
$ua->default_header( 'Authorization' => "Bearer $devkey" );

# upload files
my @files = qw(day.jpg morning.jpg night.jpg);                                                                                  
foreach my $file (@files) {                                                                                                     
        my $fqfn = $file;                                                                                                       
        $fqfn =~ s{^}{/usr/share/backgrounds/};                                                                                 
        print "$file $fqfn\n";                                                                                                  
        die "no file $file" unless -f $fqfn;                                                                                    

        my $resp = $ua->post( "https://upload.box.com/api/2.0/files/content",                                                   
                Content_Type => 'form-data',                                                                                    
                Content => [                                                                                                    
                        Filedata => [ $fqfn, $file,                                                                             
                                Content_Type => 'image/jpg',                                                                    
                                parent => $customer_dir_id,                                                                     
                        ],                                                                                                      
                ],                                                                                                              
        );                                                                                                                      
        unless ($resp->is_success) {                                                                                            
                print Dumper($resp);                                                                                            
                print "ERROR: upload $file failed\n\n";                                                                         
                print "returned " . $resp->code() . "\n";                                                                       
                my $box_resp = $json_parser->decode( $resp->decoded_content );                                                  
                print Dumper($box_resp);                                                                                        
                die "see above";                                                                                                
        }                                                                                                                       

        die "devel";                                                                                                            
}

我一直回来:

{
      'status' => 400,
      'request_id' => 'bfbfbfbfbfbfbfbfbfbfbfbfbfb',
      'type' => 'error',
      'context_info' => {
                 'errors' => [
                               {
                                 'name' => 'parent',
                                 'reason' => 'missing_parameter',
                                 'message' => '\'parent\' is required'
                               }
                             ]
                        },
      'message' => 'Bad Request',
      'help_url' => 'http://developers.box.com/docs/#errors',
      'code' => 'bad_request'
 };

注意:

  • 我正在使用1小时开发键。
  • 我对API中的其他方法没有任何问题。我可以遍历文件夹并使文件夹正常。

所以,基本上,我如何将parent参数添加到文件上传?

更新

以下是正确构建请求的代码:

    my $req = POST "https://upload.box.com/api/2.0/files/content",                                                             
            Content_Type => 'form-data',                                                                                       
            Content => [                                                                                                       
                    attributes => '{"name":"'. $file .'", "parent":{"id":"'. $customer_dir_id .'"}}',                          
                    file => [ $fqfn ],                                                                                         
            ],                                                                                                                 
    ;                                                                                                                          

    my $resp = $ua->request($req);

1 个答案:

答案 0 :(得分:3)

查看box documentation中的示例curl命令:

curl https://upload.box.com/api/2.0/files/content \
  -H "Authorization: Bearer ACCESS_TOKEN" -X POST \
  -F attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
  -F file=@myfile.jpg

这会生成一个请求:

  • Authorization标头的值为Bearer ACCESS_TOKEN
  • attributes表单字段的值为{"name":"tigers.jpeg", "parent":{"id":"11446498"}}
  • file表单字段获取文件myfile.jpg
  • 中的内容

由于LWP :: UserAgent的post方法只是HTTP::Request::Common::POST的包装器,因此很容易创建一个请求对象并检查它是否正在生成您想要的内容:

use strict;
use warnings;

use HTTP::Request::Common;

my $request = POST 'https://upload.box.com/api/2.0/files/content',
    Authorization => 'Bearer ACCESS_TOKEN',
    Content_Type  => 'form-data',
    Content       => [ attributes => '{"name":"tigers.jpeg", "parent":{"id":"11446498"}}',
                       file       => [ 'foo' ] ];

print $request->as_string;

输出:

POST https://upload.box.com/api/2.0/files/content
Authorization: Bearer ACCESS_TOKEN
Content-Length: 237
Content-Type: multipart/form-data; boundary=xYzZY

--xYzZY
Content-Disposition: form-data; name="attributes"

{"name":"tigers.jpeg", "parent":{"id":"11446498"}}
--xYzZY
Content-Disposition: form-data; name="file"; filename="foo"
Content-Type: text/plain

foo
bar
baz

--xYzZY--