实践解决-The msearch request must be terminated by a newline [\n]

本文档介绍了在使用Kibana接口获取Elasticsearch数据时遇到的‘msearch request must be terminated by a newline [\n]’错误。错误源于POST请求的payload格式不正确。解决方案是将请求参数转换为list格式,从而成功解决问题。

这里写自定义目录标题

在请求kibana中的接口获取数据
因为/kibanashow/elasticsearch/_msearch接口是post,payload请求

错误信息

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "The msearch request must be terminated by a newline [\n]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "The msearch request must be terminated by a newline [\n]"
    },
    "status": 400
}

需要header

self.headers = {'Authorization':'x x x,
                        'Content-Type':'application/json',
                        'Cookie':xxx',
                        'User-Agent':'xxx,
                        'kbn-version':'6.4.1',
                        'Host':'XXX',
                        'Accept': 'text/plain'}

在这里插入图片描述## 解决方案
请求参数变成为list格式

data = [{"index":"logstash-haproxy*","ignore_unavailable":true,"timeout":30000,"preference":1646739348568},
{"version":true,"size":500,"sort":[{"@timestamp":{"order":"desc","unmapped_type":"boolean"}}],"_source":{"excludes":[]},"aggs":{"2":{"date_histogram":{"field":"@timestamp","interval":"1w","time_zone":"Asia/Shanghai","min_doc_count":1}}},"stored_fields":["*"],"script_fields":{},"docvalue_fields":[{"field":"@timestamp","format":"date_time"}],"query":{"bool":{"must":[{"match_all":{}},{"exists":{"field":"http_status_code"}},{"match_phrase":{"http_status_code":{"query":"502"}}},{"range":{"@timestamp":{"gte":1646582400000,"lte":1647187200000,"format":"epoch_millis"}}}],"filter":[],"should":[],"must_not":[]}},"highlight":{"pre_tags":["@kibana-highlighted-field@"],"post_tags":["@/kibana-highlighted-field@"],"fields":{"*":{}},"fragment_size":2147483647}}]
#对每个json都加一个换行符
data_as_str=""
            for d in self.data:
                data_as_str += json.dumps(d)+" \n"
            res = requests.post(url=self.host + self.api, data=data_as_str, headers=self.headers,verify=False)
res = requests.post(url=self.host + self.api, data=data_as_str, headers=self.headers,verify=False)

然后就返回成功了
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值