【问题标题】:Conversion of CURL to Python requests Elastic searchCURL 到 Python 请求的转换 Elastic search
【发布时间】:2019-04-09 07:21:38
【问题描述】:

我有以下用于弹性搜索的 curl 命令:

curl -XGET "http://my_host.net:9200/_search" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } }'

我尝试使用https://curl.trillworks.com/ 将其转换为等效的python 请求。但我收到 405 作为响应。

我的python转换代码如下:

    import requests
    data = ' { "query": { "match_all": {} } }'

    response = requests.post('http://my_host.net:9200/_search -H', data=data)

【问题讨论】:

    标签: python-3.x python-2.7 curl python-requests


    【解决方案1】:

    您需要从您的post 调用中的URL 中删除-H,并将Content-type 标头添加到调用中:

    import requests
    data = ' { "query": { "match_all": {} } }'
    headers = {'Content-Type': 'application/json'}
    
    response = requests.post('http://my_host.net:9200/_search', data=data, headers=headers)
    

    【讨论】:

    • 成功了。但我很困惑,因为我的 curl 是一个 Get 请求,但它转换为 post。我怎样才能让 Get 调用这个?
    • 使用 requests.get... 虽然请注意,使用 GET 发送正文可能无法按预期工作。
    • 好的。谢谢
    • 很高兴它有帮助!
    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 2023-04-02
    • 2016-01-30
    • 2020-03-18
    • 2023-04-08
    相关资源
    最近更新 更多