簡體   English   中英

在Perl中對JSON的HTTP POST請求

[英]HTTP POST request for JSON in Perl

我正在嘗試在Linksys EA4500 AP中配置無線設置(例如將ssid設置為“ vinoth”通道設置為“ 1”)。 我可以使用wireshark看到以下POST消息。

3E@@!dPzY+2K
SPOST /JNAP/ HTTP/1.1
Host: 192.168.1.1
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:22.0) Gecko/20100101 Firefox/22.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
X-JNAP-Action: http:cisco.com/jnap/core/Transaction
X-JNAP-Authorization: Basic YWRtaW46YWRtaW4=
X-Requested-With: XMLHttpRequest
Referer: http:/cisco.com/     <<<<==== routerip
Content-Length: 474
Cookie: initial-tab=; ui-proxy-path=local; admin-auth=Basic%20YWRtaW46YWRtaW4%3D; current-applet=7B1F462B-1A78-4AF6-8FBB-0C221703BEA4
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

[{"action":"http:/cisco.com/jnap/wirelessap/SetRadioSettings","request":{"radios":[{"radioID":"RADIO_2.4GHz","settings":{"isEnabled":true,"mode":"802.11bgn","ssid":"vinoth","broadcastSSID":true,"channelWidth":"Auto","channel":1,"security":"None"}}]}},{"action":"http://cisco.com/jnap/guestnetwork/SetGuestNetworkSettings","request":{"isGuestNetworkEnabled":false,"guestSSID":"vinoth-guest","guestPassword":"BeMyGuest","maxSimultaneousGuests":5,"broadcastGuestSSID":false}}]

我嘗試使用Perl自動執行上述操作,但沒有鍛煉。 由於我是這個JSON頁面的新手,所以我不知道該如何發送POST消息。

我為$ json保存的內容是

'{"action":"http:/cisco.com/jnap/wirelessap/SetRadioSettings",' .
        '"request":{"radios":{"radioID":"RADIO_2.4GHz","settings":'.'{"isEnabled":true,"mode":"802.11bgn","ssid":"vinoth212","broadcastSSID":true,"channelWidth":"Auto","channel":1,"security":"None"}}}},{"action":"http://cisco.com/jnap/guestnetwork/SetGuestNetworkSettings",' .
    '"request":{"isGuestNetworkEnabled":false,"guestSSID":"vinoth-guest","guestPassword":"BeMyGuest","maxSimultaneousGuests":5,"broadcastGuestSSID":false}}';

和代碼:

    use strict;
    use warnings;

    use LWP;

    my $ua = LWP::UserAgent->new;
    my $ip = $self->{ip};
    my $url = "http://$ip/";
    my $json = "";

    my $req = HTTP::Request->new(POST=>$url);
    $req->header('content-type' => 'application/json');
    $req->authorization_basic("admin", "admin");
    $req->content($json);

下面的代碼用於檢查請求狀態

    my $res = $ua->request($req);
    if ($res->is_success) {
    my $message = $res->decoded_content;
    print "received the message";
    } else {
    print "HTTP get code: ", $res->code, "\n";
    print "HTTP get: msg: ", $res->message, "\n";
    }

請幫助我解決此問題。 等待您的寶貴答復。

PS:在Wireshark發布消息中,鏈接不正確,因為我無法發布包含2個以上鏈接的查詢。

謝謝,Vinoth

使用代碼$req->authorization_basic("admin", "admin"); 這意味着Authorization: Basic YWRtaW46YWRtaW4=發出任何請求時, Authorization: Basic YWRtaW46YWRtaW4= HTTP標頭。

但是從wireshhir的標頭中可以X-JNAP-Authorization: Basic YWRtaW46YWRtaW4= ,這是另一回事(自定義標頭)。 因此,從您的塊中刪除authorization_basic並使用以下代碼:

$req->header('Content-Type' => 'application/json; charset=UTF-8');
$req->header('X-JNAP-Action' => 'http:cisco.com/jnap/core/Transaction');
$req->header('X-JNAP-Authorization' => 'Basic YWRtaW46YWRtaW4=');
## YWRtaW46YWRtaW4= is base64 of admin:admin
$req->header('X-Requested-With' => 'XMLHttpRequest');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM