简体   繁体   English

如何使用LWP发出JSON POST请求?

[英]How can I make a JSON POST request with LWP?

If you try to login at https://orbit.theplanet.com/Login.aspx?url=/Default.aspx (use any username/password combination), you can see that the login credentials are sent as a non-traditional set of POST data: just a lonesome JSON string and no normal key=value pair. 如果您尝试登录https://orbit.theplanet.com/Login.aspx?url=/Default.aspx (使用任何用户名/密码组合),您可以看到登录凭据是作为非传统集发送的POST数据:只是一个寂寞的JSON字符串,没有普通的键=值对。

Specifically, instead of: 具体而言,而不是:

username=foo&password=bar

or even something like: 甚至是这样的:

json={"username":"foo","password":"bar"}

There's simply: 简单来说:

{"username":"foo","password":"bar"}

Is it possible to perform such a request with LWP or an alternative module? 是否可以使用LWP或替代模块执行此类请求? I am prepared to do so with IO::Socket but would prefer something more high-level if available. 我准备使用IO::Socket这样做,但如果可用的话,我会更喜欢更高级别的东西。

You'll need to construct the HTTP request manually and pass that to LWP. 您需要手动构建HTTP请求并将其传递给LWP。 Something like the following should do it: 像下面这样的东西应该这样做:

my $uri = 'https://orbit.theplanet.com/Login.aspx?url=/Default.aspx';
my $json = '{"username":"foo","password":"bar"}';
my $req = HTTP::Request->new( 'POST', $uri );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );

Then you can execute the request with LWP: 然后你可以用LWP执行请求:

my $lwp = LWP::UserAgent->new;
$lwp->request( $req );

Just create a POST request with that as the body, and give it to LWP. 只需创建一个POST请求,并将其作为正文,并将其提供给LWP。

my $req = HTTP::Request->new(POST => $url);
$req->content_type('application/json');
$req->content($json);

my $ua = LWP::UserAgent->new; # You might want some options here
my $res = $ua->request($req);
# $res is an HTTP::Response, see the usual LWP docs.

The page is just using an "anonymized" (without name) input, which happens to be in JSON format. 该页面只是使用“匿名”(无名称)输入,恰好是JSON格式。

You should be able to use $ua->post($url, ..., Content => $content) , which in turn use the POST() function from HTTP::Request::Common . 您应该能够使用$ ua-> post($ url,...,Content => $ content) ,而后者又使用HTTP :: Request :: Common中的POST()函数。

use LWP::UserAgent;

my $url = 'https://orbit.theplanet.com/Login.aspx?url=/Default.aspx';
my $json = '{"username": "foo", "password": "bar"}';

my $ua = new LWP::UserAgent();
$response = $ua->post($url, Content => $json);

if ( $response->is_success() ) {
    print("SUCCESSFUL LOGIN!\n");
}
else {
    print("ERROR: " . $response->status_line());
}

Alternatively, you can also use an hash for the JSON input: 或者,您也可以使用哈希作为JSON输入:

use JSON::XS qw(encode_json);

...

my %json;
$json{username} = "foo";
$json{password} = "bar";

...

$response = $ua->post($url, Content => encode_json(\%json));

If you really want to use WWW::Mechanize you can set the header 'content-type' before post 如果您真的想使用WWW :: Mechanize,可以在发布之前设置标题'content-type'

$mech->add_header( 
'content-type' => 'application/json'
);

$mech->post($uri, Content => $json);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM