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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4199266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:39:46  来源:igfitidea点击:

How can I make a JSON POST request with LWP?

perljsonhttp-postlwp

提问by Richard Sim?es

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 LWPor an alternative module? I am prepared to do so with IO::Socketbut would prefer something more high-level if available.

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

回答by friedo

You'll need to construct the HTTP request manually and pass that to LWP. Something like the following should do it:

您需要手动构建 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 );

Then you can execute the request with LWP:

然后你可以用 LWP 执行请求:

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

回答by hobbs

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.

回答by Paolo Rovelli

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

回答by TheDrev

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

如果您真的想使用 WWW::Mechanize,您可以在发布前设置标题“内容类型”

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

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