in reply to Bug in LWP? Missing cookie

The problem is solved and I still don't fully understand HTTP::Request::Common. I had written

$res = $ua->request(POST "https://panel.dreamhost.com/index.cgi", @fields);

and that was what I have been asking you folks about -- the fields were correct but it wouldn't log in. I looked more carefully at my request and theirs and mine had a content-length of zero and theirs a content-length of 76. Then I noticed that in addition to the form fields it had a "content" of all of the form fields pushed together and HTML encoded. Poring over H::R::C I noticed there is a subtle difference between passing the array of fields and passing a ref to the array of fields. So I made one, very nonintuitive to me!, change in the request:

$res = $ua->request(POST "https://panel.dreamhost.com/index.cgi", \@fields);

and it all magically works now! I had been exploring doing the direct POST construction because for other forms I needed to include a file-upload in the Contents. Now I guess I'll use it for everything in preference to $ua->post(). Whew! Thanks for all the patience and advice.

Replies are listed 'Best First'.
Re^2: Bug in LWP? Missing cookie
by bliako (Abbot) on Aug 05, 2018 at 12:23 UTC

    I am a bit confused but also interested to see how it worked. Do you want to say that: $ua->post("https://panel.dreamhost.com/index.cgi", \@fields); does not work?

      No the other way: just putting in @fields or , as it is in the docs, name=> value, name=>value, ... didn't work. What did work is using the reference: \@fields. They both send the POST variables and values, except: the reference one also includes a 'content' string which is all the variables concatenated and html encoded. That made the difference and *everything* magically worked. I wish I could explain more about what made the difference or why it worked.

      How I found it is that I compared every byte that my browser sent with what my Perl program sent. They were {essentially} identical except that I noticed that the browser had a content-length of 76 {and the data string i mentioned above} and Perl had a content-length of 0. I pored over the docs and blundered across a mention of the content-length stuff in HTTP::Request::Common, tried it and discovered that that made Perl sent the content-string and it all worked. If someone understands this bit of the POST formatting, I'd be happy to better understand what was/is going on. But for now, all I can say is that using the reference works and using the array doesn't.

        Some general thoughts. I don't know much about this general topic, but looking at HTTP::Request::Common, I see that while the discussion therein of the POST method has many text examples in the
            POST $url, Header => Value,...
            POST $url, $form_ref, Header => Value,...
        format, the actual function invocation examples all look like
            $request = HTTP::Request::Common::POST( $url, [ %data ] );
        or
            $ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]);
        etc., so this "array" (which might be better thought of as a list of paired items) is always passed as an array reference.

        Because it's easy in Perl for a function to look at its arguments at run-time and determine if an argument is a simple scalar or some kind of reference and take alternative action accordingly, functions with complex argument lists are often written so that a particular function  foo() can be called as any of
            foo($string, [ qw(list of stuff) ]);
            foo($string, $another_string, [ %hash ]);
            foo($string, $second_string, $third_string, \@array);
        etc., where the expressions  [ qw(list of stuff) ] and  [ %hash ] and  \@array all produce array references.

        BTW:   Please don't post text with fields like [some text here] because the  [ ] (square brackets) are markup and just create links to nowhere. (Update: Please see Writeup Formatting Tips and Markup in the Monastery.)


        Give a man a fish:  <%-{-{-{-<

        I agree with AnomalousMonk:

         text examples in the
            POST $url, Header => Value,...
            POST $url, $form_ref, Header => Value,...
        format, 
        
        (Update1: deleted my wrong conclusion)

        these are just text / informal examples. They are not actual Perl.

        So, the perl function invocation is   $ua->request(POST 'http://somewhere/foo', [foo => bar, bar => foo]); in your case  $ua->request(POST 'http://somewhere/foo', \@fields)

        In this Monastery there is no magic (maybe only M.A.G.I.C. or just M-A-G-I-C (re: B-I-N-G-O) ), that's why I am here

        p.s. Kudos on your meticulous investigation skills ++ (re: headers byte count).