"502 Bad Gateway" When try to change LED Status

Hello, I would like to control a LED over GPIO from a External Homepage.
When I execute the following code i get “502 Bad Gateway” error message.
What is wrong on my Code?

  <br>
  <form action="https://iotserver.pst-smartcity.ch:9999/v2/users/tadmin/devices/raspberry001/ledgreen/api?authorization=eyJhbGc...nZxE" method="POST">
  <input type="radio" name="in" id="in" value="true">Turn the GPIO on.
  <br>
  <input type="radio" name="in" id="in" value="false">Turn the GPIO off.
  <br>
  <br>
  <input type="submit" value="GPIO Signal!">
  </form>
  <iframe id="ifrm1" name="ifrm2" style="display:none"></iframe>

Here is what I see in the Trace Log:
Strange ist that on the Host it says (NO SSL) but it needs to bee SSL!

2018-04-23T16:23:27.852833 trace [HTTP Request Factory] Parsed HTTP Request!
^[[0;49;33m[HTTP REQUEST] http://iotserver.pst-smartcity.ch:9999/v2/users/tadmin/devices/raspberry001/ledgreen/api?authorization=eyJhbGciOiJIUzI1NiIsInR5cC...VnZxE^[[0m

HOST iotserver.pst-smartcity.ch:9999 (NO SSL)
POST /v2/users/tadmin/devices/raspberry001/ledgreen/api?authorization=eyJhbGciOiJIUzI1NiIsInR5cC…nZxE
Host: iotserver.pst-smartcity.ch:9999
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: http://color.pst-smartcity.ch/
Content-Type: application/x-www-form-urlencoded
Content-Length: 37
Cookie: _ga=GA1.2.221346426.1523698477; _gid=GA1.2.118800728.1524477125
Connection: keep-alive
Upgrade-Insecure-Requests: 1

in=%7B%E2%80%9Cin%E2%80%9D%3A+true%7D
2018-04-23T16:23:27.853351 trace [Http Response] Sending response…
^[[0;49;34m[HTTP RESPONSE]^[[0m

HTTP/1.1 502
Access-Control-Allow-Origin: *
Content-Length: 89
Content-Type: text/html

Bad Gateway

502 Bad Gateway

When I open the follogin url I can access the JSON Status

https://iotserver.pst-smartcity.ch:9999/v2/users/tadmin/devices/raspberry001/ledgreen/api?authorization=eyJhbGciOiJIUzI1NiIsI

Thanks for your help

Roman

Hi @SecureXperts,

we are going to patch the server to properly handle the application/x-www-form-urlencoded, and provide the real error behind the bad gateway (currently it is caused due to an unsupported content type). The server supports this content type but it is expecting an utf-8 charset). Will notify you in the following hours. After that, use the form fields as keys in the input data:

Having (notice I have removed the trailing /api from the URL, as your are controlling the resouce directly)

  <br>
  <form action="https://iotserver.pst-smartcity.ch:9999/v2/users/tadmin/devices/raspberry001/ledgreen?authorization=eyJhbGc...nZxE" method="POST">
  <input type="radio" name="state" id="state" value="on">Turn the GPIO on.
  <br>
  <input type="radio" name="state" id="state" value="off">Turn the GPIO off.
  <br>
  <br>
  <input type="submit" value="GPIO Signal!">
  </form>
  <iframe id="ifrm1" name="ifrm2" style="display:none"></iframe>

Then access the information in your device:

    thing["ledgreen"] << [](pson& in){
        const std::string& state = in["state"];
        if(state=="on"){
            // the request was on
        }else{
           // the request was off
        }
    };

This way you can send more than one parameter in the form request, i.e, the digital pin to control, PWM, etc.

Don’t worry about the HTTP (and not HTTPS) in debug… as it may be a logging error. Will try to fix that also. Will keep you updated when it is ready.

Hi,

you can test the latest server version 1.6.2 that address this issue. Now with the sample page and code i posted, you should be able to retrieve a the form request. Let me know if it works!

Best.

Hello Alvarolb,

When i update the server using this command
snap refresh thinger-maker-server
snap “thinger-maker-server” has no updates available
snap refresh thinger-maker-server --beta
snap “thinger-maker-server” has no updates available

even the betaversion has now updated version.
In the Console it shows me the following version:

1.5.15 © 2018 Copyright. Thinger.io

any Idea

Roman

Hi,

refresh the snap with:

sudo snap refresh thinger-maker-server --stable

(to avoid keeping track on beta server).

Hello,

It works like a charm :wink:
Thanks a lot for your efforts

Roman

1 Like