This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

HifiBerry REST API
#1
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/audiocontro...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
BR
Kai-Roger
Reply
#2
Try adding ['Content-Type'] = 'application/json' header
Reply
#3
(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.
BR
Kai-Roger
Reply
#4
headers = {
    ['Content-Length'] = #body,
    ['Content-Type'] = 'application/json',
  },
Reply
#5
(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.
BR
Kai-Roger
Reply
#6
Code:
body = require('json').encode({
  percent = tostring(event.getvalue())
})
Reply
#7
(10.05.2021, 06:30)admin Wrote:
Code:
body = require('json').encode({
  percent = tostring(event.getvalue())
})

Works :) Thanks for great support!
BR
Kai-Roger
Reply


Forum Jump: