使用kibana/elastic 7.9.1的API调用时无法生成短url

wbrvyc0a  于 2023-06-20  发布在  Kibana
关注(0)|答案(1)|浏览(205)

我试图使一个python脚本使用的请求包,你给予的时间范围和 Jmeter 板ID,你得到的短网址。我现在收到一个201的回复
Elastic目前运行于7.9.1版
贝娄是什么IM发送。这不是我的代码的1对1副本。我把它修剪成最小的需要。同时用X替换一些值

url = "https://SERVER:PORT/api/short_url" # I just copy the printout of the variable how i derive the value  make it hard to read but this seems to work as i get a proper response from the server 
payload = {
            "objects":[
                {
                    "type": "dashboard",
                    "id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
                    "rel": "dashboard",
                    "timeRange":{
                        "from":"2022-01-01T00:05:00.0831Z",
                        "to":"2022-02-02T00:05:00.0831Z"
                    }
                }
            ]
        }

response = requests.post(url,json=payload,verify=False)

响应给了我一个201与我复制波纹管的消息

b'{"_index":"api","_type":"short_url","_id":"FwF8fYgBlzSK0Y_fa4q3","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":10,"_primary_term":2}'

Failed to generate a short URL. Error: {"_index":"api","_type":"short_url","_id":"FwF8fYgBlzSK0Y_fa4q3","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":10,"_primary_term":2}

我应该得到一个像这样的“urlId”的响应:“f73b295ff92718b26bc94edac766d8e3”
我现在想的是payload/json不正确或缺少一些值。
谢谢你的帮助

mgdq6dx1

mgdq6dx11#

这比使用请求模块要容易得多。试试这个:

import os
from elasticsearch import Elasticsearch

USERNAME = 'username'
PASSWORD = 'password'
PORT= 'XXXX'
HOST = 'host ip'
CERT = os.path.join(os.path.dirname(__file__),"cert.crt")
PATH = ''

#Connect to server
def elastic_query():
    try:
        es = Elasticsearch(
            f"https://{HOST}:{PORT}",
            ca_certs = CERT,
            verify_certs=False,
            basic_auth = (USERNAME,PASSWORD)
        )
    except ConnectionRefusedError as error:
        print(error,"[-] Connection error")
    else:
        #Query logs using DSL language
        query_res = es.search(
            index="your_index_name",
            body={
                "from": 0,
                "size": 1,
                "sort": [
                    {
                        "@timestamp": {
                            "order": "desc",
                            "unmapped_type": "boolean"
                        }
                    }
                ],
                "_source": [
                    "agent.hostname",
                    "@timestamp"
                ],
                "query": {
                    "bool": {
                    "must": [],
                    "filter": [
                        {
                        "range": {
                            "@timestamp": {
                            "format": "strict_date_optional_time||epoch_second",
                            "gte": "now-24h",
                            "lte": "now/d" 
                            }
                        }
                        }
                    ],
                    "should": [],
                    "must_not": []
                    }
                }
                }
            )
      #retunn a general query
      print(query_res['hits']['hits'])
      #return specific key/values like timestamp here
      print(query_res['hits']['hits']['@timestamp'])
    

elastic_query()

相关问题