【问题标题】:Status code 400 on post message to influxdb向 influxdb 发布消息时的状态码 400
【发布时间】:2016-10-03 19:12:37
【问题描述】:

我正在尝试将 json 文件发布到本地主机上的 influxdb。这是代码:

import json
import requests
url = 'http://localhost:8086/write?db=mydb'
files ={'file' : open('sample.json', 'rb')}
r = requests.post(url, files=files)
print(r.text)

这是sample.json 的样子:

    {
            "region" : "eu-west-1",
            "instanceType": "m1.small"
    }

我的回复给出以下错误:

 {"error":"unable to parse '--1bee44675e8c42d8985e750b2483e0a8\r': 
 missing fields\nunable to parse 'Content-Disposition: form-data;
 name=\"file\"; filename=\"sample.json\"\r': invalid field 
 format\nunable   to parse '\r': missing fields\nunable to parse '{': 
 missing fields\nunable to parse '\"region\" : \"eu-west-1\",':  invalid 
 field format\nunable to parse '\"instanceType\": \"m1.small\"':  invalid 
 field format\nunable to parse '}': missing fields"}

我的 json 似乎是一个有效的 json 文件。我不确定我做错了什么。

【问题讨论】:

    标签: python json post influxdb http-status-code-400


    【解决方案1】:

    我认为问题可能在于您只是打开文件但没有读取它。我的意思是,既然您想发布存储在文件中的 json 对象的内容,而不是文件本身,那么这样做可能会更好:

    import json
    import requests
    url = 'http://localhost:8086/write?db=mydb'
    json_data = open('sample.json', 'rb').read()   # read the json data from the file
    r = requests.post(url, data=json_data) # post them as data
    print(r.text)
    

    这实际上是你的代码修改了一点......

    【讨论】:

    【解决方案2】:

    出于性能原因,不推荐使用 JSON 写入数据,此后已被删除。

    请参阅 GitHub 问题 cmets 107043910

    【讨论】:

    • 是的,你是对的。在我发布问题后看到了这个。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2018-11-05
    • 2013-01-02
    相关资源
    最近更新 更多