vchinnat has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to configure the wireless settings (like setting ssid as "vinoth" channel as "1") in Linksys EA4500 AP. I could see the below POST message using wireshark.

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/20100 +101 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://192.168.1.1/ Content-Length: 474 Cookie: initial-tab=; ui-proxy-path=local; admin-auth=Basic%20YWRtaW46 +YWRtaW4%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.co +m/jnap/guestnetwork/SetGuestNetworkSettings","request":{"isGuestNetwo +rkEnabled":false,"guestSSID":"vinoth-guest","guestPassword":"BeMyGues +t","maxSimultaneousGuests":5,"broadcastGuestSSID":false}}]

I tried automating the above action using Perl but it didn't workout. As I am new to this JSON page I don't know how exactly the POST message to be sent.

Below is the code I tried but got "500 Internal Server Error".

use strict; use warnings; use LWP; my $ua = LWP::UserAgent->new; my $ip = $self->{ip}; my $url = "http://$ip/"; my $json = '[{"action":"http:/cisco.com/jnap/wirelessap/SetRadioSe +ttings","request":{"radios":[{"radioID":"RADIO_2.4GHz","settings":{"i +sEnabled":true,"mode":"802.11bgn","ssid":"vinoth","broadcastSSID":tru +e,"channelWidth":"Auto","channel":1,"security":"None"}}]}},{"action": +"http://cisco.com/jnap/guestnetwork/SetGuestNetworkSettings","request +":{"isGuestNetworkEnabled":false,"guestSSID":"vinoth-guest","guestPas +sword":"BeMyGuest","maxSimultaneousGuests":5,"broadcastGuestSSID":fal +se}}] '; 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"; }

Please help me in fixing this. Awaiting for your valuable reply.

Replies are listed 'Best First'.
Re: How to Post a HTTP Request for a JSON web page?
by Anonymous Monk on Mar 26, 2014 at 07:20 UTC

      No, it didn't help me

        No, it didn't help me

        Why not? Second item is Did you output a valid CGI header first?, do you know what that means?

Re: How to Post a HTTP Request for a JSON web page?
by Anonymous Monk on Mar 26, 2014 at 08:03 UTC
    Try  use Data::Dump qw/ dd /; dd( $res->as_string );

      I'm not sure on the content ($json) variable I passed in the script. I assumed the below code works.

      my $json = '[{"action":"http:/cisco.com/jnap/wirelessap/SetRadioSe +ttings","request":{"radios":[{"radioID":"RADIO_2.4GHz","settings":{"i +sEnabled":true,"mode":"802.11bgn","ssid":"vinoth","broadcastSSID":tru +e,"channelWidth":"Auto","channel":1,"security":"None"}}]}},{"action": +"http://cisco.com/jnap/guestnetwork/SetGuestNetworkSettings","request +":{"isGuestNetworkEnabled":false,"guestSSID":"vinoth-guest","guestPas +sword":"BeMyGuest","maxSimultaneousGuests":5,"broadcastGuestSSID":fal +se}}] '; my $req = HTTP::Request->new(POST=>$url); $req->header('content-type' => 'application/json'); $req->authorization_basic("admin", "admin"); $req->content($json);

        I'm not sure on the content ($json) variable I passed in the script. I assumed the below code works.

        I didn't ask about the content of the request, I asked about the content of the response

        Either one of two things is going on, you're running this as CGI and its failing because its not a CGI (no headers like FAQ says)

        Or the response is 500 for some reason ...

        So fix the cgi headers if that is the issue

        Of examine (or show) the response to see if it says something more , to make sure it is a server response and not internal lwp response

Re: How to Post a HTTP Request for a JSON web page?
by Anonymous Monk on Mar 26, 2014 at 20:06 UTC
    Did you provide LWP::UserAgent with a cookieJar?

      The below code worked fine. I missed to have "/JNAP/" in my URL after adding all the header fields required.

      use strict; use warnings; use LWP; #creat User Agent using LWP my $ua = LWP::UserAgent->new(); my $url = "http://192.168.1.1/JNAP/"; my $json = "[{\"action\":\"http://cisco.com/jnap/wirelessap/SetRadioSe +ttings\","; $json .= "\"request\":{\"radios\":[{\"radioID\":\"RADIO_2.4GHz\",\"set +tings\":"; $json .= "{\"isEnabled\":true,\"mode\":\"802.11bgn\",\"ssid\":\"Vinodh +La\",\"broadcastSSID\":true,"; $json .= "\"channelWidth\":\"Auto\",\"channel\":6,\"security\":\"None\ +"}}]}},"; $json .= "{\"action\":\"http://cisco.com/jnap/guestnetwork/SetGuestNet +workSettings\","; $json .= "\"request\":{\"isGuestNetworkEnabled\":false,\"guestSSID\":\ +"vinoth-guest\",\"guestPassword\":\"BeMyGuest\","; $json .= "\"maxSimultaneousGuests\":5,\"broadcastGuestSSID\":false}}]" +; my $req = HTTP::Request->new(POST=>$url); $req->header('Content-Type' => 'application/json; charset=UTF-8'); $req->header('X-JNAP-Action' => 'http://cisco.com/jnap/core/Transactio +n'); $req->header('X-JNAP-Authorization' => 'Basic YWRtaW46YWRtaW4='); $req->header('X-Requested-With' => 'XMLHttpRequest'); $req->header('Referer' => "http://192.168.1.1/"); $req->content($json); my $res = $ua->request($req); if ($res->is_success) { my $message = $res->decoded_content; print "received the message"; } else { use Data::Dump qw/ dd /; dd( $res->as_string ); print "HTTP get code: ", $res->code, "\n"; print "HTTP get msg : ", $res->message, "\n"; }