Connect/disconnect command- api call syntax

What is the problem?
I'm connecting to octoprint from my HA system (fibaro)
I have it working for most commands pause, resume, etc.
I can also retrieve information successfully

Ideally provide exact steps to follow in order to reproduce your problem:
-->
For cancel the following is working.

`body = '{"command":"cancel"}'
	url = "http://192.168.1.145/api/job?apikey="..apikey
	local http = net.HTTPClient()
	http:request(url, { 
		options = { 
		method = 'POST', 
		headers = {['Content-Type'] = 'application/json'},
		data = body
  	},})`

For connect I am using the User api key but the following is not working

`body = '{"command":"connect",}'
	url = "http://192.168.1.145/api/connection?apikey="..Uapikey
	local http = net.HTTPClient()
	http:request(url, { 
		options = { 
		method = 'POST', 
		headers = {['Content-Type'] = 'application/json'},
		data = body
 	},})`

Do I have the syntax correct
Any help appreciated

What did you already try to solve it?
I also tried the following

Working perfectly

`body = '{"source":"core","action":"shutdown",}'
	url = "http://192.168.1.145/api/system/commands/core/shutdown?apikey="..apikey
	local http = net.HTTPClient()
	http:request(url, { 
		options = { 
		method = 'POST', 
		headers = {['Content-Type'] = 'application/json'},
		data = body
 	},})
end`

Not working - returning a 403

`body = '{"command":"connect",}'
	url = "http://192.168.1.145/api/connection"
	local http = net.HTTPClient()
	http:request(url, { 
		options = { 
			method = 'POST', 
			headers = {['apikey'] = '6E6D1D77F8A7450BA876973E9B6B0A84', ['Content-Type'] = 'application/json; charset=UTF-8'},
			data = body
 		},
  
  		success = function(resp)
  			if (resp.status == 200) or (resp.status == 201) then            
  				local data = json.decode(resp.data);
    			fibaro:debug(resp.data);
  			else
       			fibaro:debug(resp.status);
  				fibaro:debug("ERR");
  			end
		end, 
	
    error = function(error)
	fibaro:debug"ERROR"
  	fibaro:debug(error)
	end     
  
  })`

Also not working and returning a 400

`body = '{"command":"disconnect",}'
	url = "http://192.168.1.145/api/connection?apikey="..Uapikey
	local http = net.HTTPClient()
	http:request(url, { 
		options = { 
			method = 'POST', 
			headers = {['Content-Type'] = 'application/json; charset=UTF-8'},
			data = body
 		},
  
  		success = function(resp)
  			if (resp.status == 200) or (resp.status == 201) then            
  				local data = json.decode(resp.data);
    			fibaro:debug(resp.data);
  			else
       			fibaro:debug(resp.status);
  				fibaro:debug("ERR");
  			end
		end, 
	
   		error = function(error)
		fibaro:debug"ERROR"
  		fibaro:debug(error)
	end     
  
  })`

I'm think it's syntax issue

Additional information about your setup (OctoPrint version, OctoPi version, printer, firmware, octoprint.log, serial.log or output on terminal tab, ...)

Latest octoprint version.

What does "is not working" mean? What error do you get back? What's in the body of the error (it usually will tell you what's wrong)?

On first look the connect command looks correct and only matches the docs. However your apikey header is wrong. apikey is the name when you use it as a request parameter, but that really should only be done for debugging. When used as a header, name it X-Api-Key.

Thanks
I'll switch to x-api-key and post the errors
Should be home in a while

Hi
I've changed to using X-Api-Key in the header and using the UserAPKey for all my commands. Pause, Cancel,.Shutdown, all working okay

Disconnect and Connect still returning error

Code for disconnect

body = '{\"command\":\"disconnect\",}' 

		url = "http://192.168.1.145/api/connection"
		local http = net.HTTPClient()
		http:request(url, { 
			options = { 
				method = 'POST', 
				headers = {['X-Api-Key'] = Uapikey, ['Content-Type'] = 'application/json'},
				data = body
	 		},
      
      		success = function(resp)
      			if (resp.status == 200) or (resp.status == 201) then            
      				local data = json.decode(resp.data);
        			fibaro:debug(resp.data);
      			else
           			fibaro:debug(resp.data);		
            		fibaro:debug(resp.status);
      				fibaro:debug("ERR");
      			end
    		end, 
    	
       		error = function(error)
    		fibaro:debug"ERROR"
      		fibaro:debug(error)
    	end     
      
      })

Error as follows

I don't know anything about fibaro syntax, but just based on what you quoted as working before I'd expect it should be this:

body = '{"command":"disconnect",}' 

		url = "http://192.168.1.145/api/connection"
		local http = net.HTTPClient()
		http:request(url, { 
			options = { 
				method = 'POST', 
				headers = {['X-Api-Key'] = Uapikey, ['Content-Type'] = 'application/json'},
				data = body
	 		},
      
      		success = function(resp)
      			if (resp.status == 200) or (resp.status == 201) or (resp.status == 204) then            
      				local data = json.decode(resp.data);
        			fibaro:debug(resp.data);
      			else
           			fibaro:debug(resp.data);		
            		fibaro:debug(resp.status);
      				fibaro:debug("ERR");
      			end
    		end, 
    	
       		error = function(error)
    		fibaro:debug"ERROR"
      		fibaro:debug(error)
    	end     
      
      })

Changes: No \ in the json body (no reason to escape the " there), and HTTP 204 which the command will return on success will not be interpreted as an error - in general it probably makes more sense to test for status code >= 200 and < 300 since those are the "no error" statuses, or even < 400 if you also want to include redirects.

If this still doesn't work however, take a look into the response body. Your screenshot only contains the status line basically, this is not really helpful for debugging this.

Alternatively fire up tcpdump or ngrep on your Pi and log the commands your fibaro HA is sending to it, that might also shed some light on things.

Thanks, That didn't resolve :frowning:
Fibaro uses a version of lua as its scripting language and there are a lot of nuances and non more so than in http calls.

When I looked at your doc, the only difference I could see between a 'cancel' and a 'connect' was "Requires user rights."
Is the User API Key I'm using enough to authenticate to ensure I have the correct user rights
the user is setup as active and Admin

image

I'll take a closer look at the response body and failing that I'll look at what the Fibaro is sending it...

Thanks for the feedback and OctoPrint (of course)

Header detail in the response

status: 400
headers: table: 0x84114a0
	Pragma: no-cache
	Content-Type: text/html
	Connection: close
	Expires: -1
	Cache-Control: pre-check=0, no-cache, no-store, must-revalidate, post-check=0, max-age=0
	X-Robots-Tag: noindex, nofollow, noimageindex
	Content-Length: 192
	X-Clacks-Overhead: GNU Terry Pratchett
	Set-Cookie: session_P80=.eJyrVopPK0otzlCyKikqTdVRis9MUbKqVlJIUrJS8q1KNorMSjfxzQ3K8Kvyy_F3DzT1NQrLiQxxy4py9y33Mwqs8HXxNfR3ibRVqtVRykxJzSvJLKnUSywtyYgvqSxIVbLKK83JQZJBMj0qzysjKbekKjW8wigx3BVsQmlxalE8XjW1AEUWOqo.DbLShA.EX-Fec1Zar_gFBq51qLm2r_0CMo; Path=/; HttpOnly

Yeah, that should definitely be enough. And if it wasn't you wouldn't be getting an HTTP 400 ("Bad Request") but rather a 403 ("Forbidden"). The 400 is indicating that something in the request you are sending is malformed and that's the problem.

Sadly still not the right info :wink: The response body is what would contain the info you are looking for.