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

查看:195
本文介绍了如何使用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天全站免登陆