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

发布于 2024-10-03 00:43:56 字数 517 浏览 3 评论 0原文

如果您尝试登录 https://orbit.theplanet.com/Login.aspx?url=/ Default.aspx(使用任何用户名/密码组合),您可以看到登录凭据作为一组非传统的 POST 数据发送:只是一个孤独的 JSON 字符串,没有正常的键=值对。

具体来说,而不是:

username=foo&password=bar

甚至类似:

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

很简单:

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

是否可以使用 LWP 或替代模块执行此类请求?我准备使用 IO::Socket 来实现这一点,但如果有的话,我更喜欢更高级的东西。

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.

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? I am prepared to do so with IO::Socket but would prefer something more high-level if available.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沙与沫 2024-10-10 00:43:56

您需要手动构建 HTTP 请求并将其传递给 LWP。应该执行类似以下操作:

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 );

然后您可以使用 LWP 执行请求:

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

You'll need to construct the HTTP request manually and pass that to 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:

my $lwp = LWP::UserAgent->new;
$lwp->request( $req );
Oo萌小芽oO 2024-10-10 00:43:56

只需创建一个 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.

Just create a POST request with that as the body, and give it to 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.
套路撩心 2024-10-10 00:43:56

该页面仅使用“匿名”(无名称)输入,该输入恰好采用 JSON 格式。

您应该能够使用 $ua->post($url , ..., Content => $content),这又使用 HTTP::Request::Common

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());
}

或者,您也可以对 JSON 输入使用哈希:

use JSON::XS qw(encode_json);

...

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

...

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

The page is just using an "anonymized" (without name) input, which happens to be in JSON format.

You should be able to use $ua->post($url, ..., Content => $content), which in turn use the POST() function from HTTP::Request::Common.

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:

use JSON::XS qw(encode_json);

...

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

...

$response = $ua->post($url, Content => encode_json(\%json));
君勿笑 2024-10-10 00:43:56

如果您确实想使用 WWW::Mechanize 您可以在发布之前设置标题“content-type”

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

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

If you really want to use WWW::Mechanize you can set the header 'content-type' before post

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

$mech->post($uri, Content => $json);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文