如何使用 LWP 发出 JSON POST 请求?
如果您尝试登录 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要手动构建 HTTP 请求并将其传递给 LWP。应该执行类似以下操作:
然后您可以使用 LWP 执行请求:
You'll need to construct the HTTP request manually and pass that to LWP. Something like the following should do it:
Then you can execute the request with LWP:
只需创建一个 POST 请求并将其作为正文,然后将其交给 LWP。
Just create a POST request with that as the body, and give it to LWP.
该页面仅使用“匿名”(无名称)输入,该输入恰好采用 JSON 格式。
您应该能够使用 $ua->post($url , ..., Content => $content),这又使用 HTTP::Request::Common。
或者,您也可以对 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.
Alternatively, you can also use an hash for the JSON input:
如果您确实想使用 WWW::Mechanize 您可以在发布之前设置标题“content-type”
If you really want to use WWW::Mechanize you can set the header 'content-type' before post