SOAP Request to Wemo switch from Pebble returns status 500

373 views Asked by At

Been trying to write a pebble app for wemo switches, currently this is the code i'm using:

function WemoRequest(callback) {
    if (SOAPData === false || SOAPData === undefined) {
        console.log("Invalid SOAP data: " + JSON.stringify(SOAPData));
        return;
    }

var url = "http://192.168.1.230:49153/upnp/control/basicevent1";

try {
    var request = new XMLHttpRequest();
    request.open("POST", url, false);
    request.setRequestHeader("SOAPAction", "urn:Belkin:service:basicevent:1#GetBinaryState");
    request.setRequestHeader("Content-Type",  "text/xml; charset=utf-8");
    request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status === 200 && callback) {
            callback(request, SOAPData);
            }else{console.log("Status: "+request.status + " State: "+request.readyState+" Callback: "+callback);}
    };
    var packet = '<?xml version="1.0" encoding="utf-8"?>'+
            '<s:Envelope xmls:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'+
        '<s:Body>'+
            '<u:GetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"></u:GetBinaryState>'+
        '</s:Body>'+
    '</s:Envelope>';

    request.send(packet);
} catch (error) {
    console.log("Error in XMLHttpRequest: " + error);
}}

I currently get status 500 from OnReadyStateChange and have no idea what I'm doing wrong. If this isn't enough code, app code is available here:https://github.com/dmf444/Webble

1

There are 1 answers

0
J.D. Progger On

So...I know this is from 4 years ago lol, but I found this during a google search and just found the answer, so I figured I would respond for that reason: I think your header just needs an extra set of quotes around "urn:Belkin:service:basicevent:1#SetBinaryState" so that the string specifying the soapaction literally starts and ends with quotes.

I'm working in Python (because that's what all the kids seem to be doing these days), but I too was getting the 500 error until I made a very subtle change (the single quote marks around my double quotes) and almost cried tears of joy when my light turned off:

"SOAPACTION": '"urn:Belkin:service:basicevent:1#SetBinaryState"'

So here's the working version of the code (in Python lol):

import http.client

#Variables (value=on/off, ipaddress=address of your wemo)
value = 0 #1=ON, 0=OFF
ipAddress = "192.168.0.108"

#Build the SOAP Envelope (data)
data = '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>' + str(value) + '</BinaryState></u:SetBinaryState></s:Body></s:Envelope>'

#Build the Header (headers)
headers = {"Content-type" : 'text/xml; charset="utf-8"', "SOAPACTION": '"urn:Belkin:service:basicevent:1#SetBinaryState"', "Content-Length": len(data)}

#Send request and check response data (resp_data)
conn = http.client.HTTPConnection(ipAddress, 49153)
conn.request("POST", "/upnp/control/basicevent1", data, headers)
response = conn.getresponse()
resp_data = response.read()

if response.status == 200:
    conn.close()
    print("SUCCESS!")
elif response.status == 403:
    print("ERROR: 403 (FORBIDDEN)")
else:
    print("ERROR: " + str(response.status))