我正在使用Fiware Orion上下文代理,并且我想使用python脚本发布一些数据。命令行(工作正常)如下所示:
curl -X POST -H "Accept: application/json" -H "Fiware-ServicePath: /orion" -H "Fiware-Service: orion" -H "Content-Type: application/json" -d '{"id": "JetsonTX1", "type": "sensor", "title": {"type": "Text","value": "Init"}, "percentage": { "type": "Text", "value": "0%"}}' "http://141.39.159.63:1026/v2/entities/"
我的Python脚本:
import requests
import json
url = 'http://141.39.159.63:1026/v2/entities/'
data = '''{
"title": {
"value": "demo",
"type": "Text"
},
"percentage": {
"type": "Text",
"value": "0%"
}'''
data_json = json.dumps(data)
headers = {"Accept": "application/json", "Fiware-ServicePath": "/bonseyes", "Fiware-Service": "bonseyes", "Content-Type": "application/json"}
response = requests.post(url, data=data_json, headers=headers)
print(response.json())
这是response.json()返回的内容
{u'description': u'Errors found in incoming JSON buffer', u'error': u'ParseError'}
有什么办法解决这个问题吗?
谢谢!
答案 0 :(得分:0)
您可能不应像这样将数据作为字符串传递:
df9to10 =df[df['h'].between(9, 10, inclusive=True)]
将其作为常规命令传递:
data = '''{
"title": {
"value": "demo",
"type": "Text"
},
"percentage": {
"type": "Text",
"value": "0%"
}'''
请求库将自动为您转换此词典。另外,请确保您要使用data = {
"title": {
"value": "demo",
"type": "Text"
},
"percentage": {
"type": "Text",
"value": "0%"
}}
参数而不是data
。文档下方的专家应明确原因。
json
从您的评论看来,您应该像这样传递数据:
def post(url, data=None, json=None, **kwargs):
r"""Sends a POST request.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
因为端点需要json而不是格式编码的字节
最后也缺少花括号。
答案 1 :(得分:0)
我认为您应该尝试对OCB使用keyValues选项。它将使您的有效负载方式更短。我使用类似的python程序来更新值,因此在我的方法中请求PATCH:
#Sorting out url and payload for request
data = '{"' + attribute + '":' + value + '}'
headers = {'Content-type': 'application/json'}
url = urlOfOCB + '/v2/entities/' + entityId + '/attrs?options=keyValues'
r = requests.patch(url, (data), headers=headers)
您可以了解有关此选项here的信息。如我所见,您没有为属性定义任何新类型,因此在使用keyValues时,默认情况下它将为“文本”。
属性/元数据类型可以在请求中省略。在属性/元数据创建或更新操作中省略时,将根据值使用默认值:
有关这些内容的更多信息,您可以找到here。