How can I send POST and GET data to a Perl CGI scr

2019-01-21 23:33发布

I am trying to send a get or a post through a command-line argument. That is test the script in the command line before I test through a browser (the server has issues). I tried searching online, and I suppose I was probably using incorrect terminology because I got nothing. I know this is possible because I saw someone do it. I just don't remember how it was done.

Thanks! :)

8条回答
虎瘦雄心在
2楼-- · 2019-01-21 23:48

To test a CGI program from the command line, you fake the environment that the server creates for the program. CGI.pm has a special offline mode, but often I find it easier not to use because of the extra setup I need to do for everything else my programs typically expect.

Depending on the implementation of your script, this involves setting many environment variables, which you can do from a wrapper script that pretends to be the server:

 #!/bin/bash

 export HTTP_COOKIE=...
 export HTTP_HOST=test.example.com
 export HTTP_REFERER=...
 export HTTP_USER_AGENT=...
 export PATH_INFO=
 export QUERY_STRING=$(cat query_string);
 export REQUEST_METHOD=GET

 perl program.cgi

If you're doing this for a POST request, the environment is slightly different and you need to supply the POST data on standard input:

 #!/bin/bash

 export CONTENT_LENGTH=$(perl -e "print -s q/post_data/");
 export HTTP_COOKIE=...
 export HTTP_HOST=test.example.com
 export HTTP_REFERER=...
 export HTTP_USER_AGENT=...
 export PATH_INFO=...
 export QUERY_STRING=$(cat query_string);
 export REQUEST_METHOD=POST

 perl program.cgi < post_data

You can make this as fancy as you need and each time you want to test the program, you change up the data in the query_string or post_data files. If you don't want to do this in a shell script, it's just as easy to make a wrapper Perl script.

查看更多
对你真心纯属浪费
3楼-- · 2019-01-21 23:49

Old discussion, but I was looking for the same answers - so for those who follow - this is what I found out

RTFM! from the CGI man page ( and there is more ) DEBUGGING If you are running the script from the command line or in the perl debugger, you can pass the script a list of keywords or parameter=value pairs on the command line or from standard input (you don't have to worry about tricking your script into reading from environment variables). You can pass keywords like this:

    your_script.pl keyword1 keyword2 keyword3

or this:

   your_script.pl keyword1+keyword2+keyword3

or this:

    your_script.pl name1=value1 name2=value2

or this:

    your_script.pl name1=value1&name2=value2

To turn off this feature, use the -no_debug pragma.
查看更多
贼婆χ
4楼-- · 2019-01-21 23:51

Are you using the standard CGI module?

For example, with the following program (notice -debug in the arguments to use CGI)

#! /usr/bin/perl

use warnings;
use strict;

use CGI qw/ :standard -debug /;

print "Content-type: text/plain\n\n",
      map { $_ . " => " . param($_) . "\n" }
      param;

you feed it parameters on the command line:

$ ./prog.cgi foo=bar baz=quux
Content-type: text/plain

foo => bar
baz => quux

You can also do so via the standard input:

$ ./prog.cgi
(offline mode: enter name=value pairs on standard input; press ^D or ^Z when done)
foo=bar
baz=quux
^D
Content-type: text/plain

foo => bar
baz => quux
查看更多
趁早两清
5楼-- · 2019-01-21 23:53

In Windows, you can use VBScript to write a command line util that calls into the MS XML library:

Dim XMLHttp : Set XMLHttp = CreateObject("Microsoft.XMLHTTP")
On Error Resume Next

strIPAddress = WScript.Arguments(0)
strMACAddress = WScript.Arguments(1)
strSubnetMask = WScript.Arguments(2)

On Error Goto 0

WScript.Echo "Attempting to wake host " & strIPAddress & " on NIC " & strMACAddress &
"using netmask " & strSubnetMask

strGetUrl = http://wolService/WolService/WolService.asmx/WakeBroadcast?hostIP=" &
strIPAddress & "&macAddress=" & strMACAddress & "&subnetMask=" & strSubnetMask

XMLHttp.Open "GET", strGetUrl, False
XMLHttp.Send ""

WScript.Echo XMLHttp.ResponseText

Edit: This script sends HTTP requests and can be used from the command line. I got confused by the question 'How can I send POST and GET data to a Perl CGI script via the command line' and thought this was about sending POST and GET data to a Perl CGI script via the command line from an unspecified client OS.

查看更多
在下西门庆
6楼-- · 2019-01-21 23:55

LWP comes with ready made scripts that can be used from the command-line. Check for GET and POST scripts in your system.

查看更多
我只想做你的唯一
7楼-- · 2019-01-22 00:12

To give a cgi script post data:

$ echo -n 'a=b;c=d' | REQUEST_METHOD=POST CONTENT_LENGTH=999 perl index.cgi

To give a cgi script get data:

$ perl index.cgi 'a=b;c=d'
查看更多
登录 后发表回答