Logic Machine Forum
HifiBerry REST API - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: HifiBerry REST API (/showthread.php?tid=3354)



HifiBerry REST API - Kai-Roger - 09.05.2021

Hi.

I normally control my HifiBerry amplifiers with NodeRed and the HifiBerry API. I now try to do the same with my LM5, but i just get an error code. I want to send % volume to the amplifier. Anyone here who knows what to adjust to get this to work?

Code:
http = require('socket.http')
ltn12 = require('ltn12')

body = require('json').encode({
  percent = event.getvalue()
})


res, code = http.request({
  url = 'http://192.168.1.91:81/api/volume',
  method = 'POST',
  headers = {
    ['Content-Length'] = #body,
  },
  source = ltn12.source.string(body)
})


log(res, code)


HifiBerry API documentation:
https://github.com/hifiberry/audiocontrol2/blob/master/doc/api.md

Error:
* arg: 1
  * string:
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html>
        <head>
            <title>Error: 500 Internal Server Error</title>
            <style type="text/css">
              html {background-color: #eee; font-family: sans;}
              body {background-color: #fff; border: 1px solid #ddd;
                    padding: 15px; margin: 15px;}
              pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
            </style>
        </head>
        <body>
            <h1>Error: 500 Internal Server Error</h1>
            <p>Sorry, the requested URL <tt>'http://192.168.1.91:81/api/volume'</tt>
              caused an error:</p>
            <pre>Internal Server Error</pre>
        </body>
    </html>

* arg: 2
  * number: 500



RE: HifiBerry REST API - admin - 09.05.2021

Try adding ['Content-Type'] = 'application/json' header


RE: HifiBerry REST API - Kai-Roger - 09.05.2021

(09.05.2021, 15:57)admin Wrote: Try adding ['Content-Type'] = 'application/json' header

Sorry. I can't find out where in the script i can add that line. I only get Lua syntax error.


RE: HifiBerry REST API - Erwin van der Zwart - 09.05.2021

headers = {
    ['Content-Length'] = #body,
    ['Content-Type'] = 'application/json',
  },


RE: HifiBerry REST API - Kai-Roger - 09.05.2021

(09.05.2021, 21:01)Erwin van der Zwart Wrote: headers = {
    ['Content-Length'] = #body,
    ['Content-Type'] = 'application/json',
  },

Thanks. I was sure that i tried that, but apparently not :)


Now i found a new problem. This part of the code did not work:
Code:
body = require('json').encode({
    percent = event.getvalue()
})

But this this works:
Code:
body = '{"percent":"30"}'

It would have been nice if i could use the event.getvalue.


RE: HifiBerry REST API - admin - 10.05.2021

Code:
body = require('json').encode({
  percent = tostring(event.getvalue())
})



RE: HifiBerry REST API - Kai-Roger - 10.05.2021

(10.05.2021, 06:30)admin Wrote:
Code:
body = require('json').encode({
  percent = tostring(event.getvalue())
})

Works :) Thanks for great support!