httpPostJson question

Hi, can anyone tell me what’s wrong with my code?
I can’t receive any post json values on my server side using that method.

def params = [
    uri: "http:// abc.com/abc.php",
    body: [
        home: [homeId: "c2350d41",
        		extraData: "SmartThings"
        ]
    ]
]


try {
    httpPostJson(params) { resp ->
        resp.headers.each {
            log.debug "${it.name} : ${it.value}"
        }
        log.debug "response contentType: ${resp.    contentType}"
    }
} catch (e) {
    log.debug "something went wrong: $e"
}

Are you sure it’s being triggered? What are you seeing in the ST logs when this is triggered? If it shows success, maybe try using postcatcher to see what it’s sending just in case somethings wrong with the php you are posting to

1 Like

Tested your post with postCatcher and the results look good. So it’s likely that either it’s not triggering, or you have an issue with your PHP you are posting to

{
    "home": {
        "extraData": "SmartThings",
        "homeId": "c2350d41"
    }
}

Here’s the quick code I threw together to test your post. It’s triggered on switch change. You can use a virtual one in the simulator

/*Post Test
*/

definition(
    name: "Post Tester",
    namespace: "tierneykev",
    author: "tierneykev@gmail.com",
    description: "Test PostJson to Post Catcher",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
)

preferences {
	section("Monitor This Switch") {	
             input "theSwitch", "capability.switch", title: "Which switch?", multiple: false
	}
    section("Post to This URL") {	
                input "postCatcherID", "text", title: "postCatcher ID", required: true
	} 
    
    
}
def installed() {
	log.debug "Installed with settings: ${settings}"
    initialize()
}

def updated() {
	log.debug "Updated with settings: ${settings}"    
	initialize()
}


def initialize() {
    subscribe(theSwitch,"switch",poster)
 
}

def poster(evt){
	def url = "http://postcatcher.in/catchers/${postCatcherID}"
	def params = [
		uri: url,
		body: [
			home: [homeId: "c2350d41",
					extraData: "SmartThings"
			]
		]
	]


	try {
		httpPostJson(params) { resp ->
			resp.headers.each {
				log.debug "Header ${it.name} : ${it.value}"
			}
			log.debug "response contentType: ${resp.    contentType}"
      
		}
	} catch (e) {
		log.debug "something went wrong: $e"
 }
}

Thanks Kevin.
It works now.

Hi @Cety,

I am attempting to do something similar to what you have accomplished…but am unable to make it work. My code is slightly different with client_id and client_secret in the header…Also I am encountering a problem with content type defaulting to to “text/html” whether I specify that or not.

Hi Sainib,

If your server end using php,
try to catch all the posted data from SmartApp by using this -> php://input
see what did you get

Alternatively you can use postcatcher recommended by kevintierney to catch all the posted data from SmartApp

Thanks @Cety…It was indeed a server side issue at our end. Again, thank you.

Great :smile:
and goodluck…