GET 㨠POST ãã¼ã¿ãåãåãã«ã¯
CGI ãä½æããä¸ã§ãããã¹ãããã¯ã¹ãã©ã¸ãªãã¿ã³ãªã©ã®ãã©ã¼ã ã«å ¥åãé¸æãããç¶æ ãåãè¾¼ããã¨ã¯é¿ãã¦ã¨ããã¾ããã ããã§ã¯ãå ¥åãã©ã¼ã ã®ãã¼ã¿ãã©ã®ããã« CGI ã§åãè¾¼ãã®ããç´¹ä»ãã¾ãã åå¿è åãã«åé ã§ã§ GET 㨠POST ã«ã¤ãã¦ã解説ãã¾ãã
ãã©ã¼ã ãã¼ã¿ããµã¼ãã¼ã«éä¿¡ããæ¹æ³ (ã¡ã½ãã) ã«ã¯ GET
ã¡ã½ããã¨
POST
ã¡ã½ãããããã¾ãã
HTTP ãããã³ã«ã§ã¯ãä»ã« PUT
ã¡ã½ããã DELETE
ã¡ã½ãããè¦å®ããã¦ãã¾ããã
ãã©ã¦ã¶ã¼ã§å©ç¨ã§ããã®ã¯ GET
ã¡ã½ãã㨠POST
ã¡ã½ããã® 2 ã¤ã ãã§ãã
GET
ã¡ã½ãããã¯ãHTTP/0.9 ããè¦å®ããã¦ãããå½æã§ã¯ãã¼ã¿ããµã¼ãã¼ã«éä¿¡ããå¯ä¸ã®æ段ã§ããã
ä»ã§ããã¼ã¿éä¿¡æ段ã¨ãã¦ä½¿ããã¦ãã¾ãããéä¿¡ãã¼ã¿ã URL ã¨ãã¦ãã©ã¦ã¶ã¼ã®ã¢ãã¬ã¹ãã¼ã«è¡¨ç¤ºããã¾ãã
ãã®ããã« URL ã®ä¸é¨ã«ãªã£ã¦ãã¾ãé½åä¸ããã©ã¦ã¶ã¼ã«ãã£ã¦æåé·ã«å¶éãããã¾ãã
ä»ã¯ãã§ã«ä½¿ãããªããªãã¾ããããä¸ææã¯æ大ã®ã·ã§ã¢ãèªã£ã Microsoft Internet Explorer ã§ã¯
URL ã®æ大é·ã 2,048 æåã§ããã
ããããURL ã¨ã³ã³ã¼ã (å¾è¿°) ããç¶æ
㧠255 æåã¾ã§ã§ãã®ã§ã大ããªãã¼ã¿ãéä¿¡ããã«ã¯éçãããã¾ããã
ããã«ãéä¿¡ãã¼ã¿ã URL ã®ä¸é¨ã«ãªãã¨ãããã¨ã¯ãã¦ã§ããµã¼ãã¼ã®ãã°ã«ããã®ã¾ã¾è¨é²ããããã¨ã«ãªãã¾ãã æ¤ç´¢ãã¼ã¯ã¼ãç¨åº¦ãªããã»ã©åé¡ã¯ãªãã§ãããããå人æ å ±ã決æ¸æ å ±ãªã©ããã°ã«ãã®ã¾ã¾è¨é²ãããã®ã¯è¯ããã¨ã§ã¯ããã¾ããã
ããã解決ããã®ããæ大æå¹æåæ°ã¨ããå¶éããªã POST ã¡ã½ããã§ãã éä¿¡ãã¼ã¿ã¯ URL ã«ã»ããããã®ã§ã¯ãªããã¡ãã»ã¼ã¸ããã£ã¨ãã¦éä¿¡ããã¾ãã®ã§ã å©ç¨è ã®ç®ã«ç´æ¥è§¦ãããã¨ã¯ããã¾ããããã¦ã§ããµã¼ãã¼ã®ã¢ã¯ã»ã¹ãã°ã«ç´æ¥è¨é²ããããã¨ãããã¾ããã
GET
ã¯ä½ãããã®æ
å ±ãå¼ãåºãããã®æ段ã§ããã
ä½ãããã®ãã¼ã¿ãç»é²ãããã¢ãããã¼ããããããããã®ã¡ã½ããã§ã¯ããã¾ããã
GET
㨠POST
ã¯ãã®ååã表ãéãã®ç®çã§ä½¿ãããã«ãã¾ãããã
GET ã¡ã½ããã§ã¯ããªã¯ã¨ã¹ã URL ã®å¾ãã«éä¿¡ãã¼ã¿ãä»ãå ããå½¢ã§éä¿¡ããã¾ãã
CGI å´ã§ã¯ããã®éä¿¡ãã¼ã¿ã¯ç°å¢å¤æ° ENV{QUERY_STRING}
ã«åãããã¾ãã
次ã®ä¾ãã覧ãã ããã
<form action="https://www.futomi.com/lecture/form/exec/get-post-test.cgi" method="get">
<p>ããã¯ãã¼ã ï¼<input type="text" name="nickname" value="ããã"></p>
<p>æ§å¥ï¼
<input type="radio" name="gender" value="male" checked> ç·æ§
<input type="radio" name="gender" value="female"> 女æ§
</p>
<input type="submit" value="éä¿¡">
</form>
ä¸ã®ä¾ã§ã¯ãéä¿¡ãã¿ã³ãæ¼ã㨠$ENV{QUERY_STRING}
ã®å¤ããã®ã¾ã¾è¡¨ç¤ºãã¾ãã
ããã§æ³¨æãã¦ã»ããã®ãããã©ã¦ã¶ã¼ã®ã¢ãã¬ã¹æ¬ã«è¡¨ç¤ºããã¦ãã URL ã§ãã
次ã®ããã«ãªã£ã¦ããã¯ãã§ãã
https://www.futomi.com/lecture/form/exec/get-test.cgi?nickname=%25E3%2581%259F%25E3%2582%258D%25E3%2581%2586&gender=male
éä¿¡ãã¼ã¿ããURLã®ä¸é¨ã¨ãã¦éä¿¡ãããããã§ããéä¿¡ãã¼ã¿ã¨URL㯠?
ã§åºåããã¾ãã
ããã¦ãéä¿¡ãã¼ã¿ã®é¨åã $ENV{QUERY_STRING}
ã«æ ¼ç´ããã¾ãã
éä¿¡ãã¼ã¿ã¯ãåå
¥åé
ç®ãã¨ã« &
ã§åºåããã¾ãã
ä¾ã§ã¯ãããã¯ãã¼ã ã¨æ§å¥ã«ãããã¾ãã
ããã¯ãã¼ã ã®å¤ã¯ãããããã®ã¯ããªã®ã« %25E3%2581%259F%25E3%2582%258D%25E3%2581%2586
ã¨ãªã£ã¦ãã¾ãã
ããã¯ãã©ã¦ã¶ã¼ããã¼ã¿ãéä¿¡ããéã«ãURL ã¨ã³ã³ã¼ããããããã§ãã
GET ã¡ã½ããã§éä¿¡ããããã¼ã¿ã CGI ã§åãåãã«ã¯ã$ENV{QUERY_STRING}
ã®å¤ããä¸ã§èª¬æããããã«å解ããå¿
è¦ãããã¾ãã
ããã«ãURL ã¨ã³ã³ã¼ããããå¤ããã³ã¼ãããå¿
è¦ãããã¾ãã
POST ã¡ã½ããã§éä¿¡ããããã¼ã¿ã¯ãGET ã¡ã½ããã®ããã« URL ã®ä¸é¨ã¨ãã¦éä¿¡ãããããã§ã¯ããã¾ããã
ãããã£ã¦ãCGI ã§ã¯ $ENV{QUERY_STRING}
ãããã¼ã¿ãåãåããã¨ãã§ãã¾ããã
POST ã¡ã½ããã§éä¿¡ãããå ´åã$ENV{QUERY_STRING}
ã«ã¯ãã¼ã¿ã¯ä½ãå
¥ã£ã¦ãã¾ããã
ãã®ä»£ãããCGI ã¯ãµã¼ãã¼ã® STDIN
(æ¨æºå
¥å) çµç±ã§åãåããã¨ãã§ãã¾ãã
Perl ã¹ã¯ãªããä¸ã§ã¯ã以ä¸ã®è¨è¿°ã§åå¾ãããã¨ãã§ãã¾ãã
read( STDIN, my $data, $ENV{CONTENT_LENGTH} );
ä¸ã®ä¾ã§ã¯ãã¹ã«ã©ã¼å¤æ° $data ã«éä¿¡ãã¼ã¿ãæ ¼ç´ããã¾ãã
$ENV{CONTENT_LENGTH}
ã¯ããã©ã¦ã¶ã¼ããã®ãªã¯ã¨ã¹ããããã¼ã«ãã Content é· (ãã¤ã)ã§ãã
次ã®ä¾ãã覧ãã ããã
<form action="https://www.futomi.com/lecture/form/exec/get-post-test.cgi" method="post">
<p>ããã¯ãã¼ã ï¼<input type="text" name="nickname" value="ããã"></p>
<p>æ§å¥ï¼
<input type="radio" name="gender" value="male" checked> ç·æ§
<input type="radio" name="gender" value="female"> 女æ§
</p>
<input type="submit" value="éä¿¡">
</form>
ãã®ä¾ã¯ãåè¿°ã® GET ã¡ã½ããã®èª¬æã«ä½¿ã£ãä¾ã¨ã»ã¼åãã§ãããform
ã¿ã°ã®
method
å±æ§ã®å¤ã post
ã«æ¸ãæãããã¦ãã¾ãã
ã¹ã«ã©ã¼å¤æ° $data
ã«ã¯æ¬¡ã®å¤ãæ ¼ç´ããã¾ãã
nickname=%E3%81%9F%E3%82%8D%E3%81%86&gender=male
ããã¯ãGET ã¡ã½ããã®æã® $ENV{QUERY_STRING}
ã«æ ¼ç´ããããã¼ã¿ã¨åãã§ãã以éã®æååã®å解ã«ã¤ãã¦ã¯ GET ã¡ã½ããã®å ´åã¨åæ§ã§ãã
GET 㨠POST ã§ããã¼ã¿ã®åãåãæ¹ã«éãããããã¨ã¯ã説æããéãã§ããã 次ã®ãããªå ´åã«ã¯ã©ãããã°è¯ãã®ã§ããããã
form
ã¿ã°ã® method
å±æ§ã®æå®ã GET ãªã®ã POST ãªã®ãä¸æãããå®ç¾ããããã«ã¯ CGI å´ã§ãªã¯ã¨ã¹ãã GET ãªã®ã POST ãªã®ããåºå¥ããªããã°ããã¾ãããã
Perl ã¹ã¯ãªããä¸ã§ã¯ $ENV{REQUEST_METHOD}
ã®å¤ãè¦ããã¨ã§ã¡ã½ãããåºå¥ãããã¨ãã§ãã¾ãã
ãã®å¤ã¯ã以ä¸ã®éãã§ãã
ã¡ã½ãã | $ENV{REQUEST_METHOD} ã®å¤ |
---|---|
GET | GET |
POST | POST |
ãã®å¤ãè¦ã¦ãPerl ã¹ã¯ãªããä¸ã§å ´ååããããã°ããã®ã§ãã åè¿°ã® GET 㨠POST ã®ãµã³ãã«ã¯ãããããåã CGI ãæ å½ãã¦ãã¾ããã ã½ã¼ã¹ã³ã¼ãã¯æ¬¡ã®éãã§ãã
use strict;
use warnings;
print "Content-type: text/plain; charset=utf-8\n";
print "\n";
if ( $ENV{REQUEST_METHOD} eq "GET" ) {
print $ENV{QUERY_STRING};
}
elsif ( $ENV{REQUEST_METHOD} eq "POST" ) {
read( STDIN, my $data, $ENV{CONTENT_LENGTH} );
print $data;
}
GET 㨠POST ã®ãã¼ã¿ã®åãåãæ¹ã説æãã¾ããããåãã©ã¡ã¼ã¿ã®å解ã URL ãã³ã¼ããªã©ã æå¤ã«é¢åã¨æããã®ã§ã¯ãªãã§ããããã ãã©ã¼ã ãã¼ã¿ã®æ±ãã«ã¤ãã¦ã¯ããã§ã«åªç§ãª Perl ã¢ã¸ã¥ã¼ã« ã ã©ã¤ãã©ãªã¼ ãããã¾ãã ãããã使ã£ã¦ãã¼ã¿ãåå¾ãããã¨ããå§ããã¾ãã
Perl ã¢ã¸ã¥ã¼ã«ã¨ãã¦ã¯ãCGI.pm ãæãæåã§ãããã ãã¤ã¦ã¯ Perl5 ã«æ¨æºã¢ã¸ã¥ã¼ã«ã¨ãã¦çµã¿è¾¼ã¾ãã¦ããå®çªã¢ã¸ã¥ã¼ã«ã§ãã ææ°ã® Perl5 ã§ã¯ CGI.pm ã¯æ¨æºã¢ã¸ã¥ã¼ã«ããå¤ããã¦ãã¾ããããã¾ãªãã å¤ãã®ã¬ã³ã¿ã«ãµã¼ãã¼ã§å©ç¨ãããã¨ãã§ãã¾ãã
CGI.pm ã®ä½¿ãæ¹ã«ã¤ãã¦ã¯ãCGI.pm ã使ã£ã¦ã¿ãããã§è©³ãã解説ãã¦ãã¾ãã®ã§ã ãã²ã覧ãã ããã