Post parameters to a url

Moderator: crythias

Locked
tripti_rai2005
Znuny newbie
Posts: 67
Joined: 14 Mar 2012, 15:08
Znuny Version: OTRS2

Post parameters to a url

Post by tripti_rai2005 »

I am int process of testing the post method in perl scripting .I have made a simple cgi script to test the post method as shown below :-

!D:\perl\bin\perl.exe -wT
# Script to emulate a browser for posting to a
# CGI program with method="POST".

# Specify the URL of the page to post to.
my $URLtoPostTo = "http://localhost:78/hello.php";

# Specify the information to post, the form field name on
# the left of the => symbol and the value on the right.
my %Fields = (
"name" => "Will Bontrager",
"email" => "name\@example.com",
);

# Modules with routines for making the browser.
use LWP::UserAgent;
use HTTP::Request::Common;

# Create the browser that will post the information.
my $Browser = new LWP::UserAgent;

# Post the information to the CGI program.
my $Page = $Browser->request(POST $URLtoPostTo,\%Fields);

# Print the returned page (or an error message).
print "Content-type: text/html\n\n";
if ($Page->is_success) { print $Page->content; }
else { print $Page->message; }


In OTRS, i want to post a few parameters to a simple url for purpose of testing.Can you direct me as to how to do it.
I want to implement the same in OTRS on a simple button click.
■ TRIPTI RAI // PROJECT ENGINEER, ETIM GTEC
Office: +91 20 66056464 //Mobile: +91 9503019176 // // Whirlpool Corporation // www.WhirlpoolCorp.com
Whirlpool Corporation // www.WhirlpoolCorp.com
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Post parameters to a url

Post by reneeb »

You need

- a frontend module (Located under <OTRS_HOME>/Kernel/Modules/)
-> that module needs two methods: "new" and "Run".
-> "new" is a constructor where you set up some basic stuff and check for needed objects
-> In the "Run" method you have to decide which "Subaction" is run
-> call one Subaction "post" and another one "form"
-> when "form" is called => show the form
-> when "post" is called => post the parameters

Code: Select all

package Kernel::Modules::AgentPostParameters;

use strict;
use warnings;

our $VERSION = "0.01";

sub new{
    my ($Type, %Param) = @_;

    # allocate new hash for object
    my $Self = {%Param};
    bless( $Self, $Type );

    # check needed objects - those are passed by the OTRS module that handles the request
    # ParamObject provides methods to get the URL parameters ( see http://dev.otrs.org/cvs/Kernel/System/Web/Request.html)
    # LogObject is the interface for logging (see http://dev.otrs.org/cvs/Kernel/System/Log.html)
    # ConfigObject is needed to get sysconfig options (->Get('SysConfigOption::Name')
    for my $Needed (
        qw(ParamObject LayoutObject LogObject MainObject ConfigObject)
        )
    {
        # check if the needed object was passed
        if ( !$Self->{$Needed} ) {

            # layoutobject provides several methods needed to generate output (e.g. create dropdowns, print header/footer, ..., see http://dev.otrs.org/cvs/Kernel/Output/HTML/Layout.html)
            $Self->{LayoutObject}->FatalError( Message => "Got no $Needed!" );
        }
    }

    # here you can add new objects that are not passed or do other "setup" stuff for your module

    # return the created object
    return $Self;
}

sub Run {
    my ($Self, %Param) = @_;

    my $Suboutput = '';

    # $Self->{Subaction} is filled automatically with the value of the hidden field "Subaction"
    # $Self->{Action} is filled automatically with the value of the hidden field "Action"
    if ( !$Self->{Subaction} || $Self->{Subaction} eq 'form' ) {
        $Suboutput = $Self->{LayoutObject}->Output(
            TemplateFile => 'AgentPostParameters',  # this indicates you need a template file called AgentPostParameters.dtl
        );
    }
    elsif ( $Self->{Subaction} eq 'post' ) {
        # here you have to run your post request
    }


        # the HTML output is build here.
        my $Output = $Self->{LayoutObject}->Header(); # footer: logo, shortcuts (locked tickets, ...)
        $Output .= $Self->{LayoutObject}->NavigationBar();   # nav bar with "Dashboard", "Ticket", ..., "Admin" Links
        $Output .= $Suboutput;
        $Output .= $Self->{LayoutObject}->Footer();  # close all open HTML elements, and print footer
}

1;
- You need a template file (Kernel/Output/HTML/Standard/AgentPostParameters.dtl)

Code: Select all

<div class="MainBox ARIARoleMain LayoutFixedSidebar SidebarFirst">
    <h1>$Text{"System Log"}</h1>

    <div class="SidebarColumn">
        <div class="WidgetSimple">
            <div class="Header">
                <h2>$Text{"Hint"}</h2>
            </div>
            <div class="Content">
                With a click on the button you request a page
            </div>
       </div>
    </div>

    <div class="ContentColumn">
        <form action="$Env{"CGIHandle"}" method="post">
                    <input type="hidden" name="Action" value="$Env{"Action"}"/>
                    <input type="hidden" name="Subaction" value="post"/>
                   <button class="Primary" type="submit" value="$Text{"Save"}">$Text{"Save"}</button>
        </form>
    </div>
</div>
And you need a configuration file to have a sysconfig option to enable the frontend module (Kernel/Config/Files/Test.xml)

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<otrs_config version="1.0" init="Application">
    <ConfigItem Name="Frontend::Module###AgentPostParameters" Required="0" Valid="1">
        <Description Translatable="1">Frontend module registration for the agent interface.</Description>
        <Group>Ticket</Group>
        <SubGroup>Frontend::Agent::ModuleRegistration</SubGroup>
        <Setting>
            <FrontendModuleReg>
                <Description>Overview of all open Tickets</Description>
                <Title>QueueView</Title>
                <NavBarName>Ticket</NavBarName>
             </FrontendModulereg>
        </Setting>
    </ConfigItem>
</otrs_config>
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
tripti_rai2005
Znuny newbie
Posts: 67
Joined: 14 Mar 2012, 15:08
Znuny Version: OTRS2

Re: Post parameters to a url

Post by tripti_rai2005 »

This is how i am writing my post request in run method..please tell what am i doing wrong.I am not sure how to write a post request in run method.

sub Run {
my ( $Self, %Param ) = @_;

my $Output;

if ( $Self->{Subaction} eq 'SendValue' ) {

# challenge token check for write action
$Self->{LayoutObject}->ChallengeTokenCheck();

Action => 'Post',
URL => "Action=Post;http://localhost:78/hello1.php",
my %Fields = (
"name" => "Will Bontrager",
"email" => "name\@example.com",
);
}

else {
$Output = $Self->Form();
}
return $Output;
}
■ TRIPTI RAI // PROJECT ENGINEER, ETIM GTEC
Office: +91 20 66056464 //Mobile: +91 9503019176 // // Whirlpool Corporation // www.WhirlpoolCorp.com
Whirlpool Corporation // www.WhirlpoolCorp.com
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Post parameters to a url

Post by reneeb »

Your original code for the POST request was

Code: Select all

# Specify the URL of the page to post to.
my $URLtoPostTo = "http://localhost:78/hello.php";

# Specify the information to post, the form field name on
# the left of the => symbol and the value on the right.
my %Fields = (
"name" => "Will Bontrager",
"email" => "name\@example.com",
);

# Modules with routines for making the browser.
use LWP::UserAgent;
use HTTP::Request::Common;

# Create the browser that will post the information.
my $Browser = new LWP::UserAgent;

# Post the information to the CGI program.
my $Page = $Browser->request(POST $URLtoPostTo,\%Fields);
Compare it with your code in the Run method...

I suggest to read a Perl book: http://www.onyxneon.com/books/modern_perl/index.html
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
tripti_rai2005
Znuny newbie
Posts: 67
Joined: 14 Mar 2012, 15:08
Znuny Version: OTRS2

Re: Post parameters to a url

Post by tripti_rai2005 »

I am writing the sub run function as below and i am getting the following error
"Global symbol "%Env" requires explicit package name at D:/OTRS/OTRS//Kernel/Modu[..] "

Please help me with the sub run function i am trying to write to post parameters to a sample url.I dont have much time to learn the perl scripting and this is high priority

sub Run {
my ( $Self, %Param ) = @_;
my $ua = LWP::UserAgent->new;

my $Output;


if ( $Self->{Subaction} eq 'SendValue' ) {

<form action="$Env{"CGIHandle"}" method="post" my $URLtoPostTo ="http://localhost:78/hello.php" >
Fields => $Self->{Fields} (
"FormID" => "$QData{"FormID"}",

);
# As seen above, "@" must be escaped when quoted.

# If you want to specify a browser name,
# do so between the quotation marks.
# Otherwise, nothing between the quotes.
my $BrowserName = "";

# It's a good habit to always use the strict module.
use strict;

# Modules with routines for making the browser.
use LWP::UserAgent;
use HTTP::Request::Common;

# Create the browser that will post the information.
my $Browser = new LWP::UserAgent;

# Insert the browser name, if specified.
#if($BrowserName) { $Browser->agent($BrowserName); }

# Post the information to the CGI program.
my $Page = $Browser->request(POST $URLtoPostTo,\%Fields);

# Print the returned page (or an error message).
print "Content-type: text/html\n\n";
if ($Page->is_success) { print $Page->content; }
else { print $Page->message; }
</form>

}
■ TRIPTI RAI // PROJECT ENGINEER, ETIM GTEC
Office: +91 20 66056464 //Mobile: +91 9503019176 // // Whirlpool Corporation // www.WhirlpoolCorp.com
Whirlpool Corporation // www.WhirlpoolCorp.com
jojo
Znuny guru
Posts: 15020
Joined: 26 Jan 2007, 14:50
Znuny Version: Git Master
Contact:

Re: Post parameters to a url

Post by jojo »

http://www.perl.org/books/library.html

If you don't have time to learn the language you should not take these jobs to develop thins.

As you said it is high priority you should hire a PERL developer/freelancer
"Production": OTRS™ 8, OTRS™ 7, STORM powered by OTRS
"Testing": ((OTRS Community Edition)) and git Master

Never change Defaults.pm! :: Blog
Professional Services:: http://www.otrs.com :: enjoy@otrs.com
tripti_rai2005
Znuny newbie
Posts: 67
Joined: 14 Mar 2012, 15:08
Znuny Version: OTRS2

Re: Post parameters to a url

Post by tripti_rai2005 »

I have successfully been able to post the data..thanks for your help jojo!
■ TRIPTI RAI // PROJECT ENGINEER, ETIM GTEC
Office: +91 20 66056464 //Mobile: +91 9503019176 // // Whirlpool Corporation // www.WhirlpoolCorp.com
Whirlpool Corporation // www.WhirlpoolCorp.com
Locked