views:

599

answers:

5

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! :)

+2  A: 

Yes, it's possible to do this from the command line, bypassing your server. This page explains all: Perl CGI debugging (sitewizard.com) (Especially item 6 on that page). Here I quote the most important part:

To test the script offline using the GET method, simply set the QUERY_STRING environment variable accordingly. If you are using Windows, you might use the following command line in a DOS window prior to running the script in the same window:

set [email protected]&Fullname=M+Name

To test the script offline using the POST method, put the line below into a text file named, say, testinput.txt.

[email protected]&Fullname=M+Name

Then redirect that file as an input to the script. On Unix systems as well as under Windows' MSDOS prompt, you can do it this way:

perl -w scriptname.pl < testinput.txt

Your script will then receive that input as though it was sent it by a form on the website. Check the error messages that perl spouts, if any, to help you track the problem in the script.

amarillion
For reference, the environment-variable/standard input approach works with any CGI application, Perl or otherwise.
fennec
This advice is missing quite a bit, not to mention it's example command line has no hope of working.
brian d foy
+7  A: 

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
Greg Bacon
A: 

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

Alan Haggai Alavi
This is a different thing. He wants to run a CGI script from the command line (no server) to test it.
brian d foy
+2  A: 

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.

brian d foy
A: 

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
Luke Puplett
He wants to run a script on the command line to test it. There is no server.
brian d foy