【问题标题】:Converting Curl command line to python将 Curl 命令行转换为 python
【发布时间】:2018-08-03 15:42:40
【问题描述】:

我正在使用 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'}

任何想法如何解决这个问题?

谢谢!

【问题讨论】:

  • 我编辑的答案是否解决了您的问题?
  • 几乎!我不得不使用:response = requests.post(url, json=data, headers=headers) 而不是 response = requests.post(url, json=data_json, headers=headers) 非常感谢!
  • 如果有帮助请标记答案

标签: python json curl python-requests fiware-orion


【解决方案1】:

您可能不应该像这样将数据作为字符串传递:

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
"""

从您的评论看来,您应该像这样传递数据:

response = requests.post(url, json=data_json, headers=headers)

因为您的端点需要 json 而不是表单编码字节

最后还缺少大括号。

【讨论】:

  • 感谢您在这里帮助我。我尝试了您的建议,导致以下错误:{u'description': u'entity id length: 0, min length supported: 1', u'error': u'BadRequest'}
  • 如果您的端点需要 json 数据,请检查我的编辑,您应该使用 json 参数而不是 data
【解决方案2】:

在我看来,您应该尝试使用 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 时,默认情况下它将是“文本”。

请求中可以省略属性/元数据类型。在属性/元数据创建或更新操作中省略时,根据值使用默认值:

  • 如果 value 是字符串,则使用 Text 类型
  • 如果 value 是数字,则使用 Number 类型。
  • 如果 value 是布尔值,则使用 Boolean 类型。
  • 如果 value 是对象或数组,则使用 StructuredValue。
  • 如果 value 为 null,则使用 None。

您可以在here找到更多关于这些内容的信息。

【讨论】:

    猜你喜欢
    • 2011-05-01
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 2020-07-09
    • 2018-10-21
    • 1970-01-01
    相关资源
    最近更新 更多