无法使用api上传thingworx上的数据

时间:2018-04-10 11:32:27

标签: python http python-requests thingworx

尝试了很多方法来上传thingworx上的数据(postman,httpie等在他们的网站上给出)但不能这样做。 请查看以下代码以在thingworx上传数据:

import requests
import json
app_key = 'xxxx'
url = 'http://pp-1804040542ze.devportal.ptc.io/Thingworx/Things/lmtech_thing/Properties/humidity'
prms = {'appKey': app_key}
hdrs = {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}
data = {'humidiy': '20'}
text = json.dumps(data)
print 'data: ' + text
r = requests.put(url, params=prms, headers=hdrs, data=text)
print r.status_code

已成功创建了物品和钥匙。但它总是返回404错误。

也和邮递员一起试过。以下是截图: GET request is successful

POST request is giving 404

Trying to update the property (humidity) value in lmtech_thing

1 个答案:

答案 0 :(得分:1)

以下代码对我有用: - )

import requests  # Import requests library to send requests to Thingworx

url = 'http://52.199.28.120:8080/Thingworx/Things/work_thing/Properties/temp'
# temp is one of my property name
value = 12    # Upload 12 on Thingworx

headers = {
    'Content-Type': 'application/json',
    'appkey': 'xxxxxxxxxxxxxxxxxxxxxx',
    'Accept': 'application/json',
    'x-thingworx-session': 'true',
    'Cache-Control': 'no-cache',
}

data = {"temp": value}   # JSON data to upload on Thingworx

response = requests.put(url, headers=headers, json=data)
# Note that we have to send put request

print 'Response Code:', response.status_code
# If 200 then data has been uploaded successfully
print 'Response Content:', response.content