如何发送python请求发布等效的curl命令?

时间:2019-05-17 22:21:24

标签: python curl splunk

以下curl命令有效,并且正在尝试使用requests.post发布相同的内容(具有不同的JSON数据),并遇到以下所示的错误,有关错误之处的任何指导?

curl -vk "https://splunk-hec.company.com:8088/services/collector" -H "Authorization: {token id }" -d '{"sourcetype": "source","index":"indexname", "event": {"a": "value1", "b": ["value1_1", "value1_2"]}}'

PYTHON代码:-

_raw = {

    "Total_radar_count":"999",
    "Analyze":{
        "Screen":{"count":110,"radar_link":"change://problem/50411162&42639456&44776863&43703933"},
        "Investigate":{"count":065,"radar_link":"change://problem/50411162&42639456&44776863&43703933"},
        "Review":{"count":106,"radar_link":"change://problem/50411162&42639456&44776863&43703933"}
    },
    "timestamp": int(time.time())  # Can also use datetime.datetime.now().isoformat()
}
url = 'https://splunk-hec.company.com:8088/services/collector?sourcetype=source?index=indexname'
json = _raw
auth_token = 'token id'
head = {'Authorization': auth_token}
response = requests.post(url, json=json, headers=head)
print(response)
print (response.reason)
print(response.json())

错误:-

<Response [400]>
Bad Request
{u'text': u'No data', u'code': 5}

3 个答案:

答案 0 :(得分:0)

400(错误请求)可以有很多东西;请参阅https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400上的文档。下一步的调试步骤是摆脱wireshark,并查看两个请求之间的区别。显然,您的数据在两者之间是不同的,因此可能只是基于期望而拒绝数据。

还要检查服务器端日志。可能是真正的错误在那里。

答案 1 :(得分:0)

尝试使用

requests.post(url, headers=head, data=json.dumps(json))

您还需要导入json包,但不要担心它是内置包

答案 2 :(得分:0)

这个问题已经很老了。但是,我想分享一下,因为我认为这可能对其他人有所帮助。只需尝试为您的json发布数据添加"event":

response = requests.post(url, json={"event": json}, headers=headers)

相关问题