SergioQ has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl script that accepts a POST command from my iPhone app.

It has two params: userid and username as well as the data from a file it is sending.

Currently, for debugging (the perl script) I have to run my iPhone simulator, and go through a bunch of steps.

It would be awesome if there was a way to run my perl script from the server terminal, it would be a huge time saver. Obviously I can do it with just the username and userid, but the script fails without receiving the data from the file. And obviously that's where I need to do my debugging, the data from the file.

Is there a way to do this all from the linux terminal where my script is located?

Thank you

  • Comment on Can a POST command (with a file attachment) be simulated via the terminal?

Replies are listed 'Best First'.
Re: Can a POST command (with a file attachment) be simulated via the terminal?
by Corion (Patriarch) on Jun 05, 2024 at 06:13 UTC

    If you want to run your script on some kind of server, you can automate the POST request via curl, wget or a Perl program.

    If you want to run the script as a CGI script from the command line, you can supply the parameters on the command line:

    perl myscript.cgi userid=5378 username=Corion

    If you use some module other than CGI, please tell us which module you're using and also consider posting a relevant short self-contained example so we can easily try out and verify our approaches.

      Alternatively, if you only need to debug what the script is doing with the data and not how it is getting the data, you can create a command line flag that tells the script to get the params from a file in whatever format you want to define. If the flag is set just populate the params from the file instead of CGI or whatever you are using.

      EDIT: You can already do this with a file and 'use CGI "-debug"' if your file has lines like param1=value1. script.pl < param_file.txt

Re: Can a POST command (with a file attachment) be simulated via the terminal?
by etj (Priest) on Jun 05, 2024 at 13:37 UTC
    As an alternative to curl to actually doing it externally from the command-line, if your app uses Mojolicious then you can run it from the command-line more directly as over HTTP(s).

    To avoid XY-problem (my fingers tried to type XS there, which is a bit Freudian), I think you'll do best of all by having the file-handling in its own function, and automated tests to call that file-handling with various inputs and checking the results.

Re: Can a POST command (with a file attachment) be simulated via the terminal?
by The_Dj (Scribe) on Jun 09, 2024 at 14:11 UTC
    Is your Perl script (1) listening to the network, or is it (2) a CGI script called by Apache?

    (1) What Library are you using to listen to the network, or are you using Socket directly?
    Or (2) What CGI library are you using, or did you just code it all yourself?

    (1) You can easily use netcat in server mode (or ksh has a built in web server, afaik) or roll a quick server in perl to feed your script the test data
    or (2) you need to use set REQUEST_METHOD=GET and set "QUERY_STRING=userid=1234&username=testuser* You may also need set HTTP_COOKIE=sessid=sess_1; then your script will think the web server is feeding it the data.

    *Note that on windows, you dont' want to close the ". cmd.exe is easily confused and will usually append it to your variable, causing annoying bugs