httpPost: Any way to not parse the return data

When an HTTP Post response is received with any "text" type, it appears to be parsed as UTF-8 encoding. My external Server (Tapo device) is return a raw byte stream as the response and the protocol appears to be substituting or deleting non-printable characters. Any way to receive either the raw bytes or encoding (base64) that does not modify the information?????

Dave

Have you tried

		uri: "http://xxx.xxx.xxx.xxx",
        textParser: false,
        ...

Thanks for the reply. I reread the common methods document then ran a test (as an example). All values of textParser (true, false, none (default) result in a UTF8 stream. Next I will try varying return content-type. The type stated in the return by the Server is text/html. It is really a byte stream. (POOR coding in the server?)

Test results:

Summary

testParser TRUE
Return is as an object that I then have to extract the data from it.

[
	params:[
		uri:http://192.168.50.70:80/app/handshake1,
		body:UBgW4QQ9keGpYRgf,
		textParser:true],
	data:java.io.InputStreamReader@1557a2a, 
	encoding:UTF8, 
	parsedData:���Q��ބ����]#0;�u�����x�C; 9��2�3w�X, 
	dataArray:[-3, -3, -3, 28, 81, -3, -3, 30, -124, -3, 30, 29, -3, -3, 31, -3, 93, 35, 48, 19, 59, -3, 11, 117, -3, -3, -3, -3, 8, -3, 120, -3, 67, 59, 9, 20, 57, -3, 8, -3, 50, -3, 51, 119, -3, 88, 25, 0]
]

Note: The array elements "-3" are the UTF-8 translation of a non-printable character.

textParser: FALSE (default value)
Returns parsed data.

[
	params:[
		uri:http://192.168.50.70:80/app/handshake1, 
		body:Sd6dAbW4PVtfMubA, 
		textParser:false], 
	data: �t���w֥v��$w�|�<V�$�;jIȝ2���b�Dv��ymC, 
	dataArray:[16, 32, -3, 116, -3, -3, -3, 119, 19, -91, 118, -3, -3, 36, 119, 19, -3, 124, -3, 26, 0, 60, 4, 86, -3, 36, -3, 59, 106, 73, 29, 50, -3, -3, -3, 98, -3, 68, 118, -3, -3, 3, 121, 109, 67]]

Note that dataArray version verifies that the unpritable characters are changed to "-3"

Might try something like:

    httpGet([
        uri: "$imagePath",
        contentType: "*/*",
        headers: [
            "Cookie": cookie
        ],
        textParser: false]){ response ->
            if(debugEnabled) log.debug "${response.properties}"
            imageData = response.data 
            if(debugEnabled) log.debug "Image Size (${imageData.available()} ${response.headers['Content-Length']})"

            def bSize = imageData.available()
            def imageType = response.contentType 
            byte[] imageArr = new byte[bSize]
            imageData.read(imageArr, 0, bSize)
            if(debugEnabled) log.debug "Image size: ${imageArr.length} Type:$imageType"  
            return [iContent: imageArr, iType: imageType]
        }    

Edit: Would assume a POST response will act similarly.

1 Like

But... But... You guys are meant to know everything.... :wink: Maybe we should start a private thread.... For our masters to exchange ideas freely.... :slight_smile:

THANKS. Got me to the answer. The contentType: "/" did not work since the server states contentType: "text/html" which apparently causes the Hubitat process to then parse as plain (UTF-8) text (changing the data). But, the below worked perfectly:

Summary
def syncPost(uri, reqBody) {
	def reqParams = [
		uri: uri,
		body : reqBody,
		contentType: "text/css"
	]
	Map respData = [:]
	respData << [params: reqParams]
	httpPost(reqParams) { resp ->
		Map retData = [:]
		def data = resp.data
		def dataSize = data.available()
		byte[] dataArr = new byte[dataSize]
		data.read(dataArr, 0, dataSize)
		retData << [dataArr: dataArr]
		retData << [dataB64: dataArr.encodeBase64().toString()]
		respData << [retData: retData]
		}
	return respData
}

with a respData of:

Summary
[
	params:[
		uri:http://192.168.50.70:80/app/handshake1, 
		body:2WrLQDdD6vNAYgIO, 
		contentType:text/css], 
	retData:[
		dataArr:[-16, 7, -13, 40, 68, 100, 112, 41, 39, -66, -15, 40, 125, 15, 
				 112, 40, 82, -51, 124, 16, 0, 20, -6, -112, -55, 66, -121, -27, 
				 -17, -25, -125, -66, -23, -2, -61, -28, 4, -73, -94, 94, 81, 
				 -73, 114, 117, 49, -80, 3, 79], 
		dataB64:8AfzKERkcCknvvEofQ9wKFLNfBAAFPqQyUKH5e/ng77p/sPkBLeiXlG3cnUxsANP]
]

I store the response as base64 since decodeBase64 faithfully regenerates the array.

For others. Please not that this works in the specific case where the server is a Tapo device running the newly required KLAP protocol. Tweaking would be required to work with specific other interfaces. A true case of having to experiment to get an acceptable solution.

1 Like