1

i have set up a RESTful api on a raspberry pi which connects to my stereo to adjust the volume. it works if i test from YARC (a REST client test extension for Chrome):

http://1.2.3.4/yamaha/volume/relative/up/1/

returns this

{
  "status": "OK",
  "direction1": "up",
  "halfdecibels": "1",
  "volume": "-28.0"
}

response headers are

{
  "date": "Tue, 27 Mar 2018 21:20:53 GMT",
  "server": "Apache/2.4.25 (Raspbian)",
  "connection": "Keep-Alive",
  "keep-alive": "timeout=5, max=100",
  "content-length": "69",
  "content-type": "application/json, text/plain, */*",
  "status": 200
}

and does adjust the volume. there is a about a one second lag while the php script executes on the Raspberry.

however when i try to run from AWS Lambda, it doesn't work. it does actually adjust the volume, so the request is making it there; but the response coming back is empty.

python function on Lambda

url = "http://1.2.3.4/yamaha/" \
      "volume/relative/{}/{}/".format(direction, half_decibels)

request = urllib.request.Request(url)
try:
    response=urllib.request.urlopen(request)
    r = response.read()
    print("response from url <{}>".format(r))
    print("headers {}".format(response.headers))
    data = json.loads(r)
except Exception as e:
    print("error occurred connecting to stereo {}".format(str(e)))
    return build_volume_response(OK, "25")

the output is

response from url <b''>
headers Date: Tue, 27 Mar 2018 21:45:35 GMT
Server: Apache/2.4.25 (Raspbian)
Content-Length: 0
Connection: close

error occurred connecting to stereo Expecting value: line 1 column 1 (char 0)

(i know 'except Exception' isn't pythonic, i'm still working on poc and will put in proper error handling once i get it working)

i am suspecting a timeout of some sorts, but don't know how to troubleshoot. any ideas or suggestions?

Update: I spoofed the call out to the stereo from the php script so there is no lag; but I still get the same zero-length response. So i no longer am looking at a timeout as the root cause

Eric D'Souza
  • 680
  • 5
  • 21

1 Answers1

0

I had to set the request headers

hdr = { 'User-Agent' : 'Python-urllib/3.6', 
        'Connection' : 'keep-alive',
        'Accept' : 'application/json, text/plain, */*',
      }
req = urllib.request.Request(url, headers=hdr)

It was the 'Accept' parameter that was missing. By default only the User-Agent was set.

To troubleshoot the request headers i added this to my php script on the raspberry

        $txt = "request headers\n";
        foreach (getallheaders() as $name => $value)
        {
                $txt .= "'" . $name . "' : '" . $value . "'\n";
        }
        $myfile = file_put_contents('/var/www/logs.txt', $txt.PHP_EOL , FILE_APPEND | LOCK_EX);
        fwrite($myfile, "\n". $txt);
        fclose($myfile);
Eric D'Souza
  • 680
  • 5
  • 21