HTTPPost and HTTPPut methods problem

I have two smartthings location in separate smartthings accounts. I am trying to control a device of location b from location a through smarthings app. Following is the code that results in error “something went wrong: groovyx.net.http.HttpResponseException: Method Not Allowed”. HTTPGet method correctly works well but no luck with HTTPPost, HTTPPostJson or HTTPPut, HTTPPutJson.

---- start of code
def params = [
uri: “https://api.smartthings.com/v1/devices/deviceID/commands”,
headers: [“Content-Type”: “application/json”, Authorization: “Bearer xxxxxxxxx”],
body: [“commands”:[“component”:“main”,“capability”:“switch”,“command”:“off”,“arguments”:null]]
]

try {
httpPost(params){
resp →
resp.headers.each {
log.debug “{it.name} : {it.value}”
}
}
}
catch (e) {
log.debug “something went wrong: $e and $resp”
}
return
---- end of code

Welcome to the SmartThings Community!
Can you give more details about your Use Case, please?

Groovy SmartApps are part of our legacy platform, so I suggest you get on board with the SmartApp SDK.
As this uses NodeJS, it would be easier to create a POST request. If you have any questions about it, I can help you out.

Thanks and I am eagerly looking forward to achieving my goal with your kind help.

Actually, I am an end-user having some programming knowledge especially of Java and C++ but not an advanced programmer.

Well, the details are as follows.

  1. I have two Smartthings Accounts ‘A’ (in app on iPhone 1) and ‘B’ (in app on iPhone 2) for locations ‘A’ and ‘B’ respectively.
  2. For location ‘B’, I have generated a Personal Access Token through URL “https://account.smartthings.com/tokens
  3. I have written,published and installed a Smartapp in ‘A’, to control a device(switch on or off) in ‘B’.
  4. The groovy code given in my post is being used for the purpose. Bearer Personal Access Token generated in step 2 above is pasted in place of ‘xxxxxxxxx’ in the groovy code.
  5. deviceID in uri in the groovy code in my post, has been replaced by the actual deviceID of the target device in ‘B’.
  6. I have learned this code from Smartthings App Docs available online at URL “SmartApp — SmartThings Classic Developer Documentation”.
  7. The same method works for httpGet method but not for httpPost or httpPut methods and results in error “something went wrong: groovyx.net.http.HttpResponseException: Method Not Allowed”. This is due to the following line of code.
    body: [“commands”:[“component”:“main”,“capability”:“switch”,“command”:“off”,“arguments”:null]]
  8. I tested the httpPost by using the same code in Postman and it works from there.
    I hope these details will help you understand the scenario.

Ok, I didn’t get the error you mentioned but the “Unprocessable Entity” message. This is because the body is parsed differently. So, I suggest you sent it as a String, take a look at this sample:

def params = [
    uri: "https://api.smartthings.com/v1/devices/deviceID/commands",
    headers: [
      "Authorization": "Bearer xxxxxx",
      "Content-Type":"application/json"
    ],
    body: "{\"commands\": [{\"component\": \"main\",\"capability\": \"switch\",\"command\": \"on\",\"arguments\": null}]}"
    ]

try {
    httpPost(params) { resp ->
        log.debug "response $resp.data"
    }   
} catch (e) {
    log.debug "error: $e"
}

The printed response should be similar to:

response [results:[[id:82519d35-…, status:ACCEPTED]]]

It worked!

Thank you so much for your kind help.

1 Like