我正在尝试将REST API与Octoprint一起使用
我的代码如下:
import requests
import json
api_token = '7598509FC2184985B2B230AEE22B388F'
api_url_base = 'http://10.20.10.189/'
api_url = '{}{}'.format(api_url_base, 'api/job')
headers = {
'Content-Type': 'application/json',
'apikey': api_token,
'"command"': '"pause"',
'"action"': '"pause"'
}
response = requests.post(api_url, headers=headers)
print(response)我的结果是
<Response [400]>我现在有点迷茫。
发布于 2018-01-05 00:59:03
我正在做类似的事情。
这段代码是函数:
import json
import urllib3
ip_address = '192.168.1.55'
apikey = 'CA54B5013E8C4C4B8BE6031F436133F5'
url = "http://" + ip_address + '/api/job'
http = urllib3.PoolManager()
r = http.request('POST',
url,
headers={'Content-Type': 'application/json',
'X-Api-Key': apikey},
body=json.dumps({'command': 'pause',
'action': 'pause'}))发布于 2018-01-05 15:14:57
我从来没有使用过octoprint和request。这是从文档和知识中得到的最好的答案,通常情况下,您必须将标题和POST数据分开。
import requests
import json
api_token = '7598509FC2184985B2B230AEE22B388F'
api_url_base = 'http://10.20.10.189/'
api_url = '{}{}'.format(api_url_base, 'api/job')
headers = {
'Content-Type': 'application/json',
'X-Api-Key': api_token # the name may vary. I got it from this doc: http://docs.octoprint.org/en/master/api/job.html
}
data = {
'command': 'pause', # notice i also removed the " inside the strings
'action': 'pause'
}
response = requests.post(api_url, headers=headers, data=data)
print(response)https://stackoverflow.com/questions/46084390
复制相似问题