CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: General Discussions: General Questions:
how to send multiple arguments to perl in cgi

 



sash99
New User

Feb 6, 2017, 12:12 PM

Post #1 of 3 (1751 views)
how to send multiple arguments to perl in cgi Can't Post

hi there, can one send multiple arguments to perl script in cgi-bin
I wrote a small peril script that publishes MQTT data from a webpage. but i was wondering if I can send multiple argument. as the peril script i wrote. if you only put one argument it assumes it is payload and used the default topic, if two argument it assumes the first argument is the topic and the second the payload

i can get it to publish to the default topic but not to any secondary
in curl or browser this works for single argument
http://192.168.168.150/cgi-bin/MQTT.pl?1234

here the example of my code :

Code
#!/usr/bin/perl 
# get total arg passed to this script
BEGIN { print "Content-type: text/plain\n\n"; }
#use strict;
# MQTT Server setting and base topic
$ip = "192.168.168.150";
$port= "1883";
$tp = "/ID/";

if ($#ARGV < 0) {

print "You have not provided any arguments \n";
exit;
}

$arg1 = $ARGV[0];
$arg2 = $ARGV[1];
#$arg3 = $ARGV[2];

if ($#ARGV < 1) {
$arg2 = $arg1;
$arg1 = $tp;


}


print "\n Output:\n---------\n";
print "> MQTT Server: $ip\n\n";
print "> MQTT TOPIC: $arg1\n\n";
print "> MQTT DATA: $arg2\n\n";


system("mosquitto_pub -h $ip -p $port -t $arg1 -m $arg2");


thank you for your help it would be greatly appreciated


FishMonger
Veteran / Moderator

Feb 7, 2017, 7:21 AM

Post #2 of 3 (1746 views)
Re: [sash99] how to send multiple arguments to perl in cgi [In reply to] Can't Post

In a cgi environment args are passed as key/value pairs. Multiple pairs are separated by the & key.

Submitting these args to the script can be done with either a GET request or a POST request and would be retrieved/parsed differently. So accessing them via the @ARGV array would not be the correct method.

CGI is very outdated, but if you want to use it, you should use the CGI module. It has a param() method which is able to retrieve the args from either a GET or POST request.
https://metacpan.org/pod/distribution/CGI/lib/CGI.pod

Why did you comment out the use strict; statement? You should have that in every script.


(This post was edited by FishMonger on Feb 7, 2017, 7:22 AM)


sash99
New User

Feb 7, 2017, 9:48 AM

Post #3 of 3 (1741 views)
Re: [FishMonger] how to send multiple arguments to perl in cgi [In reply to] Can't Post

thank you fishmonger for the reply

that what i thought it was the & but it does not work, it turns out to be + that passes second argument .

A little out dated just like me -- but the little script works fine for Publishing MQTT form webpages or IOT devices that do not support MQTT directly. I use it for freeboard dash board to manipulate little devices. node-red dashboard would work but . it has a few issues that make it unusable for my needs . where as freeboard while a little harder to setup for MQTT.. it much easier to manipulate stuff then dashboard UI .. even though I am using outdated methods -- it get the job done easy enough.. :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives