2017-10-11 55 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消息。 –

相關問題