Trouble decoding HTTP response form Plex Server

I have copied someone else’s source and have a smartapp that queries the /clients api form my plex server. I get the data back, but when i try to convert the data back from base64, i get blank. The headers decode just fine. Here is my code:

/**
  • PlexApp Clients
  • Copyright 2015 Sam McLane
  • Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except
  • in compliance with the License. You may obtain a copy of the License at:
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
  • on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
  • for the specific language governing permissions and limitations under the License.

*/
definition(
name: “PlexApp Clients”,
namespace: “shmclane-plexapp”,
author: “Sam McLane”,
description: “Try to get a plex client listing in smart thigns”,
category: “My Apps”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)

preferences {
section(“Title”) {
// TODO: put inputs here
}
}

def installed() {
log.debug “Installed with settings: ${settings}”

initialize()

}

def updated() {
log.debug “Updated with settings: ${settings}”

unsubscribe()
initialize()

}

def initialize() {
// TODO: subscribe to attributes, devices, locations, etc.
//subscribe(location, null, locationHandler, [filterEvents:false])
subscribe(app, changeMode)
}

def changeMode(evt) {
def deviceNetworkId = “AC100107:7E90” // "172.16.1.7:32400"
def ip = “172.16.1.7:32400”

sendHubCommand(new physicalgraph.device.HubAction(""“GET /servers HTTP/1.1\r\nHOST: $ip\r\n\r\n”"", physicalgraph.device.Protocol.LAN, “${deviceNetworkId}”))

// log.debug location.hubs*.firmwareVersionString.findAll { it }
}

def locationHandler(evt) {
def description = evt.description
def hub = evt?.hubId

//log.debug "cp desc: " + description
//log.debug description
def parsedEvent = parseEventMessage(description) 
//parsedEvent << ["hub":hub]

log.debug "parsedEvent " + parsedEvent

if (parsedEvent.headers && parsedEvent.body)
{
	def headerString = new String(parsedEvent.headers.decodeBase64())
	def bodyString = new String(parsedEvent.body.decodeBase64())
    
    log.debug "Headers: " + headerString
	log.debug "Body: " + bodyString
    
	//def type = (headerString =~ /Content-Type:.*/) ? (headerString =~ /Content-Type:.*/)[0] : null
}

}

private def parseEventMessage(Map event) {
//handles sonos attribute events
return event
}

private def parseEventMessage(String description) {
def event = [:]
def parts = description.split(’,’)
parts.each { part ->
part = part.trim()
if (part.startsWith(‘devicetype:’)) {
def valueString = part.split(":")[1].trim()
event.devicetype = valueString
}
else if (part.startsWith(‘mac:’)) {
def valueString = part.split(":")[1].trim()
if (valueString) {
event.mac = valueString
}
}
else if (part.startsWith(‘networkAddress:’)) {
def valueString = part.split(":")[1].trim()
if (valueString) {
event.ip = valueString
}
}
else if (part.startsWith(‘deviceAddress:’)) {
def valueString = part.split(":")[1].trim()
if (valueString) {
event.port = valueString
}
}
else if (part.startsWith(‘headers:’)) {
part -= "headers:"
def valueString = part.trim()
if (valueString) {
event.headers = valueString
}
}
else if (part.startsWith(‘body:’)) {
part -= "body:"
def valueString = new String(part.trim())
//def valueString = new String(part)
//log.debug "Raw Body: " + valueString.decodeBase64()
if (valueString) {
event.body = valueString
}
}
}

event

}

And here are the debug logs:

e4e0c787-0335-4b30-8852-dda8dacad246 3:42:44 PM: debug Body:

e4e0c787-0335-4b30-8852-dda8dacad246 3:42:44 PM: debug Headers: HTTP/1.1 200 OK
Content-Type: text/xml;charset=utf-8
Content-Length: 252
Connection: close
X-Plex-Protocol: 1.0
Cache-Control: no-cache
e4e0c787-0335-4b30-8852-dda8dacad246 3:42:44 PM: debug parsedEvent [mac:6470020020B8, headers:SFRUUC8xLjEgMjAwIE9LDQpDb250ZW50LVR5cGU6IHRleHQveG1sO2NoYXJzZXQ9dXRmLTgNCkNvbnRlbnQtTGVuZ3RoOiAyNTINCkNvbm5lY3Rpb246IGNsb3NlDQpYLVBsZXgtUHJvdG9jb2w6IDEuMA0KQ2FjaGUtQ29udHJvbDogbm8tY2FjaGU=, body:PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPE1lZGlhQ29udGFpbmVyIHNpemU9IjEiPgo8U2VydmVyIG5hbWU9InRhcmRpcyIgaG9zdD0iMTcyLjE2LjEuNyIgYWRkcmVzcz0iMTcyLjE2LjEuNyIgcG9ydD0iMzI0MDAiIG1hY2hpbmVJZGVudGlmaWVyPSI3MWI3ZWIyZGZkMzcyZGY1MWI3OWNjZGQ4YTU3ZDdiZGFmYmE5ZjBjIiB2ZXJzaW9uPSIwLjkuMTEuMS42NzgtYzQ4ZmZkMiIgLz4KPC9NZWRpYUNvbnRhaW5lcj4K]
8a2a2e54-e45b-49a1-a880-0f9cbe6f312c 3:42:44 PM: trace SONOS REPONSE TYPE: Content-Type: text/xml;charset=utf-8
e4e0c787-0335-4b30-8852-dda8dacad246 3:42:43 PM: debug Executing GET /servers HTTP/1.1

The data is correct in the response. Any help is appreciated.

Throw a ‘return event’ at the end of private def parseEventMessage(String description) {

Mate, this was 3 years ago :smile:

Sorry it took so long !
:wink:

1 Like