[英]LWP POST request not working
if
語句向我顯示有響應,但是當我嘗試print
響應時,我什么也沒得到
use LWP::UserAgent;
use strict;
use warnings;
use HTTP::Request::Common;
# use this {"process": "mobileGps","phone": "9565551236"}
my $url = "the url goes here";
my $json = '{data :{"process" : "mobileGps", "phone" : "9565551236"}}';
my $req = HTTP::Request->new( POST => $url );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );
my $ua = LWP::UserAgent->new;
my $res = $ua->request( $req );
if ( $res->is_success ) {
print "It worked";
print $res->decoded_content;
}
else {
print $res->code;
}
我確實有URL:在本示例中,我只是將其取出。
我想念什么?
嘗試像這樣調試腳本:
use strict;
use warnings;
use HTTP::Request::Common;
use LWP::ConsoleLogger::Easy qw( debug_ua );
use LWP::UserAgent;
# use this {"process": "mobileGps","phone": "9565551236"}
my $url = "the url goes here";
my $json = '{data :{"process" : "mobileGps", "phone" : "9565551236"}}';
my $req = HTTP::Request->new(POST => $url);
$req->header('Content-Type' =>'application/json');
$req->content($json);
my $ua = LWP::UserAgent->new;
debug_ua( $ua );
my $res = $ua->request($req);
if ($res->is_success) {
print "It worked";
print $res->decoded_content;
} else {
print $res->code;
}
這樣(希望)可以使您更好地了解正在發生的事情。
您是否可以不使用調試器,或者添加一些打印語句來查看程序的進度?
如果不是這樣,那將是另一種在線逐行調試的情況,除OP外沒有人受益,並且最終診斷是他們應該首先學習該語言。
互聯網可能是明智的選擇,但它將使手工藝者比工匠更多
請不要指望在繪制草圖時全力以赴,然后用繩子扎到世界其他地方來完成您的工作。 這需要巨大的經驗,資質量,並了解得到甚至"What's your name" .. "Hello"
節目的工作,事情只會變得更難其后
如果您不喜歡細心和周到,而是希望別人為您做事而不是通過實驗找到解決方案,那么您就是經理,而不是程序員。 我希望您永遠不要嘗試通過出色的委派來促進軟件事業,因為這不適用於軟件
這里。 隨便使用它。 世界上到處都是管理人員。 我們需要的是優秀的程序員
use strict;
use warnings 'all';
use feature 'say';
use constant URL => 'http://example.com/';
use LWP;
my $ua = LWP::UserAgent->new;
my $json = '{}';
my $req = HTTP::Request->new( POST => URL );
$req->header( content_type => 'application/json' );
$req->content( $json );
my $res = $ua->request( $req );
say $res->as_string;
代碼很好。 問題必須出在服務於狀態代碼200的服務器上。您應該在服務器端進行檢查。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.