Httppost response parse json

Hi,

I am trying to parse the json output from a httppost.

here’s my code :

def auth() {

	def params = [
    uri: "https://accounts.api.anki.com",
    path: "/1/sessions",
    headers: ["User-Agent": "f'Vector-sdk/{anki_vector.__version__}'", "Anki-App-Key": "aung2ieCho3aiph7Eexxxx"],
    body: [
        username: "test@gmail.com",
        password: "testtest"
    ]
    ]
    
    def result = [:]

    try {
    httpPost(params) { resp ->
        resp.headers.each {
            log.debug "${it.name} : ${it.value}"
        }

        log.debug "response contentType: ${resp.contentType}"
        log.debug "response : ${resp.data}"

        result="${resp.data}"
        log.debug $sonuc

    }
    } catch (e) {
    log.debug "something went wrong: $e"
    }
}

the log output is like:

4e37b388-b4e2-429e-b434-ea521a70dddd 12:53:09 AM: debug response : {“session”:{“session_token”:“2xxxPvzAaj3n6Ejmwjd3xxx”,“user_id”:“2xxx2CH3TxxxxpJStrqptVC”,“scope”:“user”,“time_created”:“2018-12-15T21:53:09.582712433Z”,“time_expires”:“2019-12-15T21:53:09.582699756Z”},“user”:{“user_id”:“2xxx2CH3T5JxxxJStrqptVC”,“drive_guest_id”:“axxxce22-16d6-4afc-be77-e250dd286be1”,“player_id”:“axxxce22-16d6-4afc-be77-e250dd286be1”,“created_by_app_name”:“chewie”,“created_by_app_version”:“1.1.1”,“created_by_app_platform”:“android 8.1.0; samsung SM-N960F",“dob”:“1976-05-10”,“email”:"test@gmail.com",“family_name”:null,“gender”:null,“given_name”:null,“username”:"test@gmail.com”,“email_is_verified”:true,“email_failure_code”:null,“email_lang”:“en-US”,“password_is_complex”:true,“status”:“active”,“time_created”:“2018-11-08T14:04:03Z”,“deactivation_reason”:null,“purge_reason”:null,“email_is_blocked”:false,“no_autodelete”:false,“is_email_account”:true}}
4e37b388-b4e2-429e-b434-ea521a70dddd 12:53:09 AM: debug response contentType: text/json
4e37b388-b4e2-429e-b434-ea521a70dddd 12:53:09 AM: debug Connection : keep-alive
4e37b388-b4e2-429e-b434-ea521a70dddd 12:53:09 AM: debug Vary : Accept-Encoding
4e37b388-b4e2-429e-b434-ea521a70dddd 12:53:09 AM: debug Vary : Origin
4e37b388-b4e2-429e-b434-ea521a70dddd 12:53:09 AM: debug Date : Sat, 15 Dec 2018 21:53:09 GMT
4e37b388-b4e2-429e-b434-ea521a70dddd 12:53:09 AM: debug Content-Type : text/json
4e37b388-b4e2-429e-b434-ea521a70dddd 12:53:09 AM: debug Anki-Request-Id : 95u2qjct6mdddx8ajp6xyjtr

here, I want to get the session->session_token by parsing the data from response.
However I could not parse it. If I try to read as “resp.data.session.session” it complains that this is a byteArrayInputStream

how can I get the “session_token” here ?

Below is some code that works for my integration. The critical component for you may be
def jsonSlurper = new groovy.json.JsonSlurper()
cmdResponse = jsonSlurper.parseText(resp.data.result.responseData)

def sendDeviceCmd(appServerUrl, deviceId, command) {
	def cmdResponse = ""
	def cmdBody = [
		method: "passthrough",
		params: [
			deviceId: deviceId,
			requestData: "${command}"
		]
	]
	def sendCmdParams = [
		uri: "${appServerUrl}/?token=${state.TpLinkToken}",
		requestContentType: 'application/json',
		contentType: 'application/json',
		headers: ['Accept':'application/json; version=1, */*; q=0.01'],
		body : new groovy.json.JsonBuilder(cmdBody).toString()
	]
	httpPostJson(sendCmdParams) {resp ->
		if (resp.status == 200 && resp.data.error_code == 0) {
			def jsonSlurper = new groovy.json.JsonSlurper()
			cmdResponse = jsonSlurper.parseText(resp.data.result.responseData)
		} else if (resp.status != 200) {
			state.currentError = resp.statusLine
			cmdResponse = "ERROR: ${resp.statusLine}"
			sendEvent(name: "currentError", value: resp.data)
		} else if (resp.data.error_code != 0) {
			state.currentError = resp.data
			cmdResponse = "ERROR: ${resp.data.msg}"
			sendEvent(name: "currentError", value: resp.data)
		}
	}

thanks.
I tried that as :

httpPost(params) { resp →
resp.headers.each {
log.debug “${it.name} : ${it.value}”
}

log.debug "response contentType: ${resp.contentType}"
log.debug "response : ${resp.data}"
if (resp.status == 200 && resp.data.error_code == 0) {
def jsonSlurper = new groovy.json.JsonSlurper()
		cmdResponse = jsonSlurper.parseText(resp.data.session.session_token)
    log.debug cmdResponse
    }
    else {
    log.debug "not OK"
    }

but it does not accept the response as text. log says :

4e37b388-b4e2-429e-b434-ea521a70ccad 8:50:56 PM: debug response : {“session”:{“session_token”:“2gUMxjxxfJS6hDmq4xxbUzE”,“user_id”:“2gAQ2xx3T5JwxpJStrqptVC”,“scope”:“user”,“time_created”:“2018-12-16T17:50:56.570814233Z”,“time_expires”:“2019-12-16T17:50:56.570802532Z”},“user”:{“user_id”:“2gAQ2xx3T5JwxpJStrqptVC”,“drive_guest_id”:“a634ce22-xxd6-4afc-be77-e250dd286be1”,“player_id”:“a634ce22-16d6-4afc-be77-e250dd286be1”,“created_by_app_name”:“chewie”,“created_by_app_version”:“1.1.1”,“created_by_app_platform”:“android 8.1.0; samsung SM-N960F",“dob”:“1976-05-10”,“email”:"test@gmail.com,“family_name”:null,“gender”:null,“given_name”:null,“username”:"test@gmail.com",“email_is_verified”:true,“email_failure_code”:null,“email_lang”:“en-US”,“password_is_complex”:true,“status”:“active”,“time_created”:“2018-11-08T14:04:03Z”,“deactivation_reason”:null,“purge_reason”:null,“email_is_blocked”:false,“no_autodelete”:false,“is_email_account”:true}}

4e37b388-b4e2-429e-b434-ea521a70ccad 8:50:56 PM: debug Executing saytext

4e37b388-b4e2-429e-b434-ea521a70ccad 8:50:56 PM: debug something went wrong: groovy.lang.MissingPropertyException: No such property: error_code for class: java.io.ByteArrayInputStream

for some reason it accepts the response data as ByteArrayInputStream
why ?

interestingly; following change made the solution work:

httpPost(params) { resp ->

if (resp.data) {
	def data = resp.data.text
	def pData = new groovy.json.JsonSlurper().parseText(data)
    log.debug pData.session.session_token
}