2017-10-11 4 views

답변

1

일부 사이트에 다중/관련 몸을 전송하는 방법 중 일부 데모 펄 5. https://cloud.google.com/storage/docs/json_api/v1/how-tos/multipart-upload

이 필요합니다. 이것이 Google API에서 요구하는 데이터를 정확하게 전송하는지는 모르겠지만 아이디어를 제공해야합니다. MIME에 대한 기본적인 이해, 특히 다중 부분/관련이 하나의 예일뿐 아니라 MIME에서 다중 부분 메시지의 구성과 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에 대한 세부 사항을 숨기는 것이 더 좋을 것입니다. 프로덕션 코드로 사용하는 것이 좋습니다. 그러나이 코드를 사용하면 multipart 메시지 자체를 구성하는 것이 더 쉬워지며 multipart가 작동하는 방식을 더 잘 볼 수 있으며 원시 MIME 메시지를 표시하는 API 설명서에 나와있는 예제와 더 잘 관련 될 수 있습니다. –