libwww-perl-5.813 > HTTP::Request::Common
libwww-perl-5.813

名前

HTTP::Request::Common - Construct common HTTP::Request objects

HTTP::Request::Common - 汎用のHTTP::Request オブジェクトの組み立て

概要

  use HTTP::Request::Common;
  $ua = LWP::UserAgent->new;
  $ua->request(GET 'http://www.sn.no/');
  $ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);

説明

This module provide functions that return newly created HTTP::Request objects. These functions are usually more convenient to use than the standard HTTP::Request constructor for the most common requests. The following functions are provided:

このモジュールは新しく作成された HTTP::Request オブジェクトを 返す関数を提供します。 これらの関数はこれらの一般的なリクエストのための標準の HTTP::Request コンストラクタよりも、通常より使いやすいものです。 以下の関数が提供されます:

GET $url
GET $url, Header => Value,...

The GET() function returns an HTTP::Request object initialized with the "GET" method and the specified URL. It is roughly equivalent to the following call

GET() 関数は GET メソッドと指定された URL で初期化された HTTP::Request オブジェクトを返します。 それはおおざっぱには以下の呼び出しと同じです:

  HTTP::Request->new(
     GET => $url,
     HTTP::Headers->new(Header => Value,...),
  )

but is less cluttered. What is different is that a header named Content will initialize the content part of the request instead of setting a header field. Note that GET requests should normally not have a content, so this hack makes more sense for the PUT() and POST() functions described below.

しかし散らかりは少ないです。 違うところは、Content という名前のヘッダはヘッダフィールドを 設定するのではなく、リクエストの内容として初期化されます。 GET リクエストは普通内容を持たないので、後述する PUT() メソッドと POST() メソッドをハックする方が理にかなっていることに注意してください。

The get(...) method of LWP::UserAgent exists as a shortcut for $ua->request(GET ...).

LWP::UserAgent の get(...) メソッドは、$ua->request(GET ...) への ショートカットとして存在しています。

HEAD $url
HEAD $url, Header => Value,...

Like GET() but the method in the request is "HEAD".

GET() と同様ですが、このメソッドのリクエストは "HEAD" です。

The head(...) method of "LWP::UserAgent" exists as a shortcut for $ua->request(HEAD ...).

LWP::UserAgent の head(...) メソッドは $ua->request(HEAD ...) への ショートカットとして存在しています。

PUT $url
PUT $url, Header => Value,...
PUT $url, Header => Value,..., Content => $content

Like GET() but the method in the request is "PUT".

GET() と同様ですが、このメソッドのリクエストは "PUT" です。

The content of the request can be specified using the "Content" pseudo-header. This steals a bit of the header field namespace as there is no way to directly specify a header that is actually called "Content". If you really need this you must update the request returned in a separate statement.

このリクエストの内容は "Content" 疑似ヘッダを使って指定できます。 これはヘッダフィールド名前空間をほんの少し借用しているので、 本当に "Content" という名前のヘッダを直接指定する方法はありません。 これが本当に必要なら、別々の文で返されたリクエストを 更新しなければなりません。

POST $url
POST $url, Header => Value,...
POST $url, $form_ref, Header => Value,...
POST $url, Header => Value,..., Content => $form_ref
POST $url, Header => Value,..., Content => $content

This works mostly like PUT() with "POST" as the method, but this function also takes a second optional array or hash reference parameter $form_ref. As for PUT() the content can also be specified directly using the "Content" pseudo-header, and you may also provide the $form_ref this way.

これは "POST" をメソッドとしてほとんど PUT() のように機能します。 しかしこの関数は 2 番目のオプションの配列またはハッシュリファレンスの パラメータ $form_ref を取ります。 PUT() では "Content" 疑似ヘッダを使って直接指定することもできますが、 この方法で $form_ref を提供することもできます。

The $form_ref argument can be used to pass key/value pairs for the form content. By default we will initialize a request using the application/x-www-form-urlencoded content type. This means that you can emulate a HTML <form> POSTing like this:

$form_ref 引数はフォームコンテントのためのキー/値の組を渡すために 使うことが出来ます。 デフォルトでは application/x-www-form-urlencoded コンテントタイプを 使ってリクエストを初期化します。 つまり以下のようにして HTML <form> POSTすることを エミュレートできます:

  POST 'http://www.perl.org/survey.cgi',
       [ name   => 'Gisle Aas',
         email  => 'gisle@aas.no',
         gender => 'M',
         born   => '1964',
         perc   => '3%',
       ];

This will create a HTTP::Request object that looks like this:

これは以下のような HTTP::Request オブジェクトを作成します:

  POST http://www.perl.org/survey.cgi
  Content-Length: 66
  Content-Type: application/x-www-form-urlencoded

  name=Gisle%20Aas&email=gisle%40aas.no&gender=M&born=1964&perc=3%25

Multivalued form fields can be specified by either repeating the field name or by passing the value as an array reference.

複数の値を持つフォームフィールドは、フィールド名を繰り返すか、 配列リファレンスを渡すことで指定できます。

The POST method also supports the multipart/form-data content used for Form-based File Upload as specified in RFC 1867. You trigger this content format by specifying a content type of 'form-data' as one of the request headers. If one of the values in the $form_ref is an array reference, then it is treated as a file part specification with the following interpretation:

POST メソッドはRFC1867 で示された Form-based File Upload のために使われる multipart/form-data コンテントもサポートします。 リクエストヘッダの一つとして 'form-data' のコンテントタイプを 指定することにより、このコンテントフォーマットを利用することが出来ます。 もし $form_ref の中の値の1つが配列リファレンスであれば、それは以下の解釈で ファイル部分の指定であるように扱われます:

  [ $file, $filename, Header => Value... ]
  [ undef, $filename, Header => Value,..., Content => $content ]

The first value in the array ($file) is the name of a file to open. This file will be read and its content placed in the request. The routine will croak if the file can't be opened. Use an undef as $file value if you want to specify the content directly with a Content header. The $filename is the filename to report in the request. If this value is undefined, then the basename of the $file will be used. You can specify an empty string as $filename if you want to suppress sending the filename when you provide a $file value.

配列での先頭の値 ($file) はオープンするファイルの名前です。 このファイルは読みこまれ、その内容がリクエストに入れられます。 もしファイルをオープンできなければルーチンは croak します。 コンテントを直接 Content ヘッダで指定したければ $file の値を undef に してください。 $filename はリクエストで報告されるファイル名です。 この値が未定義であれば、$file の基本名が使われます。 $file の値を提供したとき、ファイル名の送信をよくせいしたいなら、 $filename に空文字列を指定することができます。

If a $file is provided by no Content-Type header, then Content-Type and Content-Encoding will be filled in automatically with the values returned by LWP::MediaTypes::guess_media_type()

Content-Type なしで $file が提供された場合、Content-TypeContent-Encoding は LWP::MediaTypes::guess_media_type() の 返り値を使って自動的に埋められます。

Sending my ~/.profile to the survey used as example above can be achieved by this:

上記の例として ~/.profile を survey に送信することが以下のようにして 実現できます:

  POST 'http://www.perl.org/survey.cgi',
       Content_Type => 'form-data',
       Content      => [ name  => 'Gisle Aas',
                         email => 'gisle@aas.no',
                         gender => 'M',
                         born   => '1964',
                         init   => ["$ENV{HOME}/.profile"],
                       ]

This will create a HTTP::Request object that almost looks this (the boundary and the content of your ~/.profile is likely to be different):

これはおおむね以下のような HTTP::Request オブジェクトを作成します (バウンダリと ~/.profile の中身は違っていることでしょう):

  POST http://www.perl.org/survey.cgi
  Content-Length: 388
  Content-Type: multipart/form-data; boundary="6G+f"

  --6G+f
  Content-Disposition: form-data; name="name"

  Gisle Aas
  --6G+f
  Content-Disposition: form-data; name="email"

  gisle@aas.no
  --6G+f
  Content-Disposition: form-data; name="gender"

  M
  --6G+f
  Content-Disposition: form-data; name="born"

  1964
  --6G+f
  Content-Disposition: form-data; name="init"; filename=".profile"
  Content-Type: text/plain

  PATH=/local/perl/bin:$PATH
  export PATH

  --6G+f--

If you set the $DYNAMIC_FILE_UPLOAD variable (exportable) to some TRUE value, then you get back a request object with a subroutine closure as the content attribute. This subroutine will read the content of any files on demand and return it in suitable chunks. This allow you to upload arbitrary big files without using lots of memory. You can even upload infinite files like /dev/audio if you wish; however, if the file is not a plain file, there will be no Content-Length header defined for the request. Not all servers (or server applications) like this. Also, if the file(s) change in size between the time the Content-Length is calculated and the time that the last chunk is delivered, the subroutine will Croak.

(エクスポート可能な) $DYNAMIC_FILE 変数を TRUE に設定すると、content 属性として サブルーチンクロージャでリクエストオブジェクトを取得します。 このサブルーチンは命令のなかのすべてのファイルの内容を読みこみ、それを 適切なチャンクにいれて返します。 これにより大量のメモリを使わずに任意の大きなファイルを アップロードすることができます。 お望みであれば /dev/audio のような無限大のファイルを アップロードすることすら可能です; しかし、ファイルが普通のファイルでない場合、 リクエストのために定義された Content-Length ヘッダがありません。 すべてのサーバ(またはサーバアプリケーション)が そのようであるというわけではありません。 また、Content-Length が計算された時点と最後のチャンクを受け取った時点で ファイルサイズが異なる場合、サブルーチンは Croak します。

The post(...) method of "LWP::UserAgent" exists as a shortcut for $ua->request(POST ...).

"LWP::UserAgent" の post(...) メソッドは $ua->request(POST ...) への ショートカットとして存在しています。

SEE ALSO

HTTP::Request, LWP::UserAgent

コピーライト

Copyright 1997-2004, Gisle Aas

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.