Post to https using perl script

Discussion in 'Perl Misc' started by cardsharper, Sep 25, 2006.

  1. cardsharper

    cardsharper Guest

    Hi

    I wanted to post / upload a file to one of our https server

    what i am trying to do here is trying to loging to the
    https server using the user credentials . then trying to
    post a file to another url which in general accepts post
    after loging in .

    i was able to successfully login, but unable to post
    is this something todo with cookies ?

    the error its throughing is "302 Found"

    ------------------here is the code --------------------------

    #!/usr/bin/perl
    use LWP;
    my $url = 'https://192.168.1.3'; # Yes, HTTPS!
    my $browser = LWP::UserAgent->new;
    my $response = $browser->get($url);
    die "Error at $url\n ", $response->status_line, "\n Aborting"
    unless $response->is_success;
    print "Whee, it worked! I got that ",
    $response->content_type, " document!\n";

    my $response1 = $browser->post( $url,
    [
    number => xxxxx,
    password => yyyyyyy,
    ],
    );


    my $url2 = 'https://192.168.1.3/postfile';

    my $response2 = $browser->post ( $url2,
    [

    filename =>['/tmp/abc.txt'],
    ],
    );

    die "Error at $url2\n ", $response2->status_line, "\n Aborting"
    unless $response2->is_success;
     
    cardsharper, Sep 25, 2006
    #1
    1. Advertising

  2. cardsharper

    Guest

    cardsharper wrote:
    > i was able to successfully login, but unable to post
    > is this something todo with cookies ?


    If you suspect a problem with cookies, did you read the LWP::UserAgent
    docs to see how cookies are handled? It says:
    >> The default is to have no cookie_jar


    If you site requires cookies, and you don't enable a cookie-jar, how
    could that work?

    FWIW, most folks would do this type of thing (these days) with
    WWW::Mechanize (which is built on LWP but is often preferable - and it
    furnishes a cookie-jar (memory resident) by default.

    --
    David Filmer (http://DavidFilmer.com)
     
    , Sep 25, 2006
    #2
    1. Advertising

  3. cardsharper

    cardsharper Guest

    Well i did look at cookies as soon as i posted so tried the following
    unable to see the file uploaded how can i debug this
    i am pretty new to http protocol ..

    #!/usr/bin/perl

    use Data::Dumper;
    use LWP::UserAgent;
    use HTTP::Cookies;

    my $https_login = 'https://192.168.1.3';
    my $https_post = 'https://192.168.1.3/post';


    my $https_user = 'xxxxx';
    my $https_pass = ''yyyyy;

    &postHTTPS();


    sub postHTTPS
    {
    #secure login

    my $ua = LWP::UserAgent->new();

    $ua->protocols_allowed( [ 'https'] );

    $ua->cookie_jar(HTTP::Cookies->new(file =>
    ".cookies.txt",autosave => 1));

    my $response = $ua->post($https_login, [ 'user' =>
    "$https_user",'password' => "$https_pass", 'switch' => 'Log In' ] );

    #print Dumper($response);

    if ($response->is_error())
    {
    printf " %s\n", $response->status_line;
    print "https request error!\n";
    } else {
    my $content = $response->content();
    print "$content\n";
    }


    $response = $ua->post($https_post, [
    filename =>['/tmp/abc.txt'],
    ], );

    if ($response->is_error())
    {
    printf " %s\n", $response->status_line;
    print "https request error!\n";
    } else {
    my $content = $response->content();
    print "$content\n";
    }
    }



    wrote:
    > cardsharper wrote:
    > > i was able to successfully login, but unable to post
    > > is this something todo with cookies ?

    >
    > If you suspect a problem with cookies, did you read the LWP::UserAgent
    > docs to see how cookies are handled? It says:
    > >> The default is to have no cookie_jar

    >
    > If you site requires cookies, and you don't enable a cookie-jar, how
    > could that work?
    >
    > FWIW, most folks would do this type of thing (these days) with
    > WWW::Mechanize (which is built on LWP but is often preferable - and it
    > furnishes a cookie-jar (memory resident) by default.
    >
    > --
    > David Filmer (http://DavidFilmer.com)
     
    cardsharper, Sep 25, 2006
    #3
  4. cardsharper

    cardsharper Guest

    hmmm looked at the webserver log .. and found this
    "POST / HTTP/1.1" 302 95 "-" "libwww-perl/5.805"

    anyidea where i am going wrong with the code ?
     
    cardsharper, Sep 25, 2006
    #4
  5. "cardsharper" <> writes:

    > hmmm looked at the webserver log .. and found this
    > "POST / HTTP/1.1" 302 95 "-" "libwww-perl/5.805"
    >
    > anyidea where i am going wrong with the code ?


    What code? There's no code there, just a line from your web server log.

    A line, I should add, that doesn't indicate an error. What makes you think
    something might be going wrong?

    sherm--

    --
    Web Hosting by West Virginians, for West Virginians: http://wv-www.net
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Sep 25, 2006
    #5
  6. cardsharper

    Guest

    cardsharper wrote:

    > $response = $ua->post($https_post, [
    > filename =>['/tmp/abc.txt'],
    > ], );


    Time to spend some more time with the docs:

    >>> The POST method also supports the multipart/form-data content
    >>> used for Form-based File Upload as specified in RFC 1867.
    >>> You trigger this content format by specifying a content type of
    >>> 'form-data' as one of the request headers.


    This is the last time I will quote the docs for you (and I've really
    done it two times too many). Please RTFM before posting questions in
    usenet.

    Why aren't you using WWW::Mechanize? Most folks find this module so
    much easier for this type of thing...

    --
    David Filmer (http://DavidFilmer.com)
     
    , Sep 25, 2006
    #6
  7. cardsharper

    zentara Guest

    On 25 Sep 2006 13:28:32 -0700, "cardsharper" <>
    wrote:

    >Hi
    >
    >I wanted to post / upload a file to one of our https server
    >
    >what i am trying to do here is trying to loging to the
    >https server using the user credentials . then trying to
    >post a file to another url which in general accepts post
    >after loging in .
    >
    >i was able to successfully login, but unable to post
    >is this something todo with cookies ?
    >
    >the error its throughing is "302 Found"


    I don't think it has anything to do with cookies. When
    you do a post, you need to setup a request object first.
    The login and post can all be done in one step.
    Try this, it works for me.

    #!/usr/bin/perl
    use warnings;
    use strict;
    use LWP::UserAgent;
    use HTTP::Cookies;
    use HTTP::Request::Common qw(POST);

    my $https_url = 'https://192.168.0.1/cgi-bin/uploads/up1.cgi';

    my $https_user = 'xxxxx';
    my $https_pass = 'yyyyy';
    my $file = 'testout.tgz';

    &postHTTPS();

    sub postHTTPS
    {
    my $ua = new LWP::UserAgent;
    $ua->protocols_allowed( [ 'https'] );
    $ua->cookie_jar(HTTP::Cookies->new(file
    =>".cookies.txt",autosave => 1));

    #setup request
    my $req = POST($https_url,
    Content_Type => 'multipart/form-data',

    Content =>[
    file =>[ $file ],
    ], );
    #setup auth
    $req->authorization_basic($https_user, $https_pass);

    #do post
    my $response = $ua->request($req);
    if ($response->is_error())
    {
    printf " %s\n", $response->status_line;
    print "https request error!\n";
    } else {
    my $content = $response->content();
    print "$content\n";
    }

    if ( $response->is_success ) {
    print $response->as_string;
    }else {
    print $response->status_line;
    }

    }
    __END__



    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html
     
    zentara, Sep 26, 2006
    #7
  8. cardsharper

    Juha Laiho Guest

    "cardsharper" <> said:
    >I wanted to post / upload a file to one of our https server
    >
    >what i am trying to do here is trying to loging to the
    >https server using the user credentials . then trying to
    >post a file to another url which in general accepts post
    >after loging in .
    >
    >i was able to successfully login, but unable to post
    >is this something todo with cookies ?
    >
    >the error its throughing is "302 Found"


    "302 Found" is not an error. It is a response status (error codes
    are also reponse status codes, but not all response status codes
    are error codes).

    Here's what HTTP documentation (IETF RFC 2616) says about 302:

    10.3.3 302 Found

    The requested resource resides temporarily under a different URI.
    Since the redirection might be altered on occasion, the client SHOULD
    continue to use the Request-URI for future requests. This response is
    only cacheable if indicated by a Cache-Control or Expires header field.

    The temporary URI SHOULD be given by the Location field in the
    response. Unless the request method was HEAD, the entity of the
    response SHOULD contain a short hypertext note with a hyperlink to
    the new URI(s).

    If the 302 status code is received in response to a request other than
    GET or HEAD, the user agent MUST NOT automatically redirect the request
    unless it can be confirmed by the user, since this might change the
    conditions under which the request was issued.
    Note: RFC 1945 and RFC 2068 specify that the client is not allowed
    to change the method on the redirected request. However, most
    existing user agent implementations treat 302 as if it were a 303
    response, performing a GET on the Location field-value regardless
    of the original request method. The status codes 303 and 307 have
    been added for servers that wish to make unambiguously clear which
    kind of reaction is expected of the client.

    This is what is commonly called a "redirect". You're given the address
    at which the requested resource actually resides, and your code should
    handle the access to the correct resource.

    You seem to be unsure about cookies, and you should be. If the site
    uses cookies for access control, your code should also handle cookies.
    Upon login (depending on the login method, though) the server creates
    a session, and will place the session identifier into a cookie. To
    be associated to that same session (and thus, to be an authenticated
    request), all requests from the same client must contain the provided
    session cookie.

    In the above, the redirect may well point to the login page (as it seems
    your code is not passing any cookie).
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
     
    Juha Laiho, Sep 29, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,558
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,841
  3. Axel
    Replies:
    8
    Views:
    1,224
    Adrienne Boswell
    Apr 27, 2009
  4. jotto
    Replies:
    4
    Views:
    431
    jotto
    Oct 2, 2006
  5. vikrant
    Replies:
    4
    Views:
    293
Loading...

Share This Page