请帮我解决这个问题。
我有一个像这样的.net核心客户端:
var client = new RestClient();
client.BaseUrl = new Uri(Host);
client.AddDefaultHeader("Content-Type", "application/json");
var request = new RestRequest();
request.Resource = "_search";
request.AddJsonBody(queryDslKibana);
request.Method = Method.POST;
request.AddHeader("Content-Type", "application/json");
request.RequestFormat = DataFormat.Json;
queryDslKibana如下:
{"query":{"match":{"message":".Txt"}}}
It runs on postman gracefully but the response on .net is:
{
"error": {
"root_cause": [{
"type": "parsing_exception",
"reason": "Expected [START_OBJECT] but found [VALUE_STRING]",
"line": 1,
"col": 1
}],
"type": "parsing_exception",
"reason": "Expected [START_OBJECT] but found [VALUE_STRING]",
"line": 1,
"col": 1
},
"status": 400
}
请帮忙 :)
在我看来,变量“queryDslKibana”没有合适的JSON格式,当使用方法“AddJsonBody()”时,对象具有适当的格式是很重要的。 “AddJsonBody()”方法序列化您发送的对象,因此您应该首先尝试匿名对象。
像这样的东西:
var requestObject = new {query = new {match = new {message = ".txt"}}};
这应该会产生你需要的JSON:
{"query": {"match": {"message": ". Txt"}}}
谢谢@michael。
最终的代码是:
到kibana api到_search端点。
问题是.net RestClient,因为我必须像你说的那样发送一个对象(匿名对象或强类型对象)...
代码的答案是:
var client = new RestClient();
client.BaseUrl = new Uri(Host);
client.AddDefaultHeader("Content-Type", "application/json");
var request = new RestRequest();
request.Resource = "_search";
//{"query":{"match":{"message":"SEPP"}}}
request.AddJsonBody(new { query = new { match = new { message = "SEPP" } } });
request.Method = Method.POST;
request.AddHeader("Content-Type", "application/json");
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.ExecutePostTaskAsync(request).Result;