?

Log in

No account? Create an account
HI... - LiveJournal Client Discussions [entries|archive|friends|userinfo]
LiveJournal Client Discussions

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

HI... [Oct. 23rd, 2002|07:04 pm]
LiveJournal Client Discussions

lj_clients

[kimera]
[mood |curiouscurious]

(After mis-posting this to lj_dev...)

Can anyone tell me why (or point me in the right direction to the reason why) the following code...


#!/usr/bin/perl

use LWP 5.64;
use HTTP::Response;
use strict;

my $url = "http://www.livejournal.com/interface/flat";
my $ida;
my $passwd;
my $mode = "login";
my $entry;
my $newline;
my $browser = LWP::UserAgent->new;
my $response;

print "Enter your id: ";
$ida = <STDIN>;

print "Enter your password: ";
$passwd = <STDIN>;

print "Enter your text:\n";
while ($newline ne ".") {
	$newline = <STDIN>;
	$newline =~ s/\n//g;
	$entry = "$entry\r\n$newline";
}

$ida =~ s/\n//g;
$passwd =~ s/\n//g;
chop($entry);
$entry =~ s/([^A-Za-z0-9 ])/sprintf("%%%02lx", unpack("c",$1))/ge;
$entry =~ s/ /+/g;


$response = $browser->post ( $url,
	[
		'mode' => '\"$mode\"',
		'user' => '\"$ida\"',
		'password' => '\"$passwd\"',
		'event' => '\"$entry\"'
	],
);

print $response->content;

exit;


...gives me the following error...

errmsg
Client error: No username sent.
success
FAIL


I've looked at the Developer Area and I can't find a single reason that it can't get the username. I haven't dont the hexcoding for non-alphanumeric characters yet (I have done this at this point), but would that affect the basic post string? I'm not giving up, but I'm lost on where to look next. Any and all help would be appreciated -- and I apologize if this is bad protocol for a first post.
linkReply

Comments:
[User Picture]From: markpasc
2002-10-23 01:40 pm (UTC)

Single-quoted strings

Variables in single-quoted strings aren't evaluated, so you shouldn't use them when building your POST query. You end up sending literally "$ida" for the username, regardless of what you type. This worked for me:

$response = $browser->post ( $url,
    [
        'mode' => $mode,
        'user' => $ida,
        'password' => $passwd,
        'event' => $entry,
    ],
);

(Reply) (Thread)
[User Picture]From: kimera
2002-10-23 01:50 pm (UTC)

Re: Single-quoted strings

Then where do I put the quotation marks around the values[1] like it says to do on the "LiveJournal Protocol" page, or can I get away with not using them thanks to LWP?

[1] Note that you must quote all values or the values can interfere with the encoding form. For example, what if somebody's password was "blah&=2+&something=yeah".
(Reply) (Parent) (Thread)
[User Picture]From: kimera
2002-10-23 02:07 pm (UTC)

w00t!

Thank you, thank you! IT works! I appreciate it, man. Thanks.
(Reply) (Parent) (Thread)
[User Picture]From: markpasc
2002-10-23 02:46 pm (UTC)

Re: Single-quoted strings

I think that should read "Note that you must URL encode all values", as the rest of the paragraph, the bulleted list, and the next paragraph are all about URL encoding. (Someone should write a patch!)

Relatedly, you're using that unpack regex to do your URL encoding (that's the usual name for that hex coding for nonalphanumerics), but LWP does encode form content you pass in as a hash. You can see you get double encoding if you change your code to:

use HTTP::Response;
use HTTP::Request::Common;
 
...
 
my $request = HTTP::Request::Common::POST( $url,
    [
        'mode' => $mode,
        'user' => $ida,
        'password' => $passwd,
        'event' => $entry,
    ],
);
print $request->content();
$response = $browser->request($request);

Removing the regex doing the unpack should work OK.

(Reply) (Parent) (Thread)
[User Picture]From: kimera
2002-10-23 03:24 pm (UTC)

Re: Single-quoted strings

Regarding hexidecimal encoding, I realized that I was over-killing it and removed the line. If you're interested, the final/working product (for thisa specific client) is here:

http://www.livejournal.com/talkread.bml?journal=turbov21&itemid=537

I really, really appreciate your help. Thank you.
(Reply) (Parent) (Thread)
[User Picture]From: markpasc
2002-10-23 03:40 pm (UTC)

Re: Single-quoted strings

Great! You're welcome.
(Reply) (Parent) (Thread)