Advertisement

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

阅读量:

这里写自定义目录标题

    • 错误信息

在请求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
    }
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

需要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'}
    
    
      
      
      
      
      
      
      
    
    AI写代码

在这里插入图片描述## 解决方案
请求参数变成为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)
    
    
      
      
      
      
      
      
      
    
    AI写代码
复制代码
    res = requests.post(url=self.host + self.api, data=data_as_str, headers=self.headers,verify=False)
    
    
      
    
    AI写代码

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

全部评论 (0)

还没有任何评论哟~