'transform curl command to pycurl

I have a working cURL command that I want to execute in a python script via pycurl. Unfortunately I can't get this to work. I am grateful for any help.

The curl command (octoprint command disconnect printer)

curl -s -H "Content-Type: application/json" -H "X-Api-Key:blablabla" -X POST -d '{ "command":"disconnect" }' http://localhost:5000/api/connection

My pycurl attempt

import pycurl
import json
 
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://192.168.178.157:5000/api/connection")
c.setopt(c.POST, 1)
c.setopt(pycurl.HTTPHEADER, ['X-Api-Key: "blablabla"'])
data = json.dumps({ "command":"disconnect" })
c.setopt(pycurl.POSTFIELDS,data)
c.perform()
c.close()

When I run this interactively in python3, I get the error message:

{"error": "You don't have the permission to access the requested resource. It is either read-protected or not readable by the server."}

What am I doing wrong?

Thanks Sepp



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source