如何使用 LWP 发出 JSON POST 请求? [英] How can I make a JSON POST request with LWP?

查看:41
本文介绍了如何使用 LWP 发出 JSON POST 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

具体来说,而不是:

username=foo&password=bar

甚至类似:

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

很简单:

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

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

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.

推荐答案

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

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

然后就可以用LWP执行请求了:

Then you can execute the request with LWP:

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

这篇关于如何使用 LWP 发出 JSON POST 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆