2017-10-11 54 views
-1

如何将一个multipart/related消息的两部分发送到HTTP(S)服务器?如何将多部分/相关消息发布到HTTP服务器?

我需要这个https://cloud.google.com/storage/docs/json_api/v1/how-tos/multipart-upload

我们使用Perl 5

+0

你到目前为止试过了哪些代码?请在您的问题中添加[mcve] –

+0

https://www.google.cn/search?q=cpan+HTTP+POST+multipart – Quentin

+0

@Quentin当我需要链接时,您的链接指向'multipart/form-data' '多部分/ related'。 – porton

回答

1

了如何将多部分/相关主体发送到一些网站的一些演示。我不知道这是否会发送完整的Google API所需的数据,但它应该给出您的想法。仍然建议您对MIME有基本的了解,特别是MIME中多部分消息的构造(其中multipart/related仅仅是一个例子)和Content-Transfer-Encoding。 Wikipedia entry to MIME可能是一个很好的开始。

use strict; 
use warnings; 
use LWP; 
use MIME::Base64 'encode_base64'; 
use HTTP::Request; 

# Create the parts, each consisting of MIME-Header and body. 
my $part1 = 
    "Content-type: application/json; charset=UTF-8\r\n\r\n". 
    "some json here\r\n"; 
my $part2 = 
    "Content-type: image/gif\r\nContent-Transfer-Encoding: base64\r\n\r\n". 
    encode_base64("...image data here..."); 

# Combine the parts to a single multipart, using the boundary defined later 
# in the Content-Type. 
my $body = 
    "--some-boundary\r\n".  # start of 1st part 
    $part1. 
    "--some-boundary\r\n".  # start of 2nd part 
    $part2. 
    "--some-boundary--\r\n"; # end boundary 

# Create the request. The Content-type is multiplart/related and defines 
# the boundary used to separate the parts. 
my $req = HTTP::Request->new(
    POST => 'http://example.com/api/postit', 
    [ 
     'Content-length' => length($body), 
     'Content-type' => 'multipart/related; boundary="some-boundary"', 
    ], 
    $body 
); 
LWP::UserAgent->new->request($req); 
+0

最好使用'MIME :: Tools'和'MIME :: Entity',就像有人提到的 – porton

+0

@porton:实际上我是这样建议的。在某种程度上,隐藏MIME的一些细节会更好,我会建议为生产代码做这些。但另一方面,使用此代码构建多部分消息本身,可以更好地了解这种多部分工作方式,并且可以更好地将其与API文档中给出的示例联系起来,该文档显示了原始MIME消息。 –

相关问题