7

I am trying to write code in C# for PUT and GET to my elasticsearch data. I typed code for PUT like this, and it seems that it works:

string url = "http://localhost:9200/my_index/my_type/";

JsonDocFormat json = new JsonDocFormat()
{
   name = "John"
};

string s = JsonConvert.SerializeObject(json);

using (var client = new WebClient())
{
   client.UploadString(url, "POST", s);
}

But I can't write code for this GET:

GET my_index/my_type/_search
{
   "query" : {
       "match" : {
            "name" : "john"
        }
    }
}

I tried something like this:

string url_req = "http://localhost:9200/my_index/my_type/_search?pretty";

string s1 = "{\"query\": {\"match\": { \"name\" : \"john\" }}}";

string s_req = url_req + s1;

using (var client = new WebClient())
{
    Console.Write(client.DownloadString(s_req));
}

But this code returned same output as for this GET:

GET /my_index/my_type/_search

It did not throw any error, but it absolutely ignored json body at the end of URL. I want to write this without any external package (as NEST or Elasticsearch.NET), just with HTTP.

Thanks in advance for any help!

5
  • shouldn't string s1 be "{\"query\": {\"match\": { \"name\" : \"john\" }}}" ? Commented Oct 28, 2015 at 20:15
  • Sorry just a mistake.. I tried to make it easier to understand. In my original document I use field ´text´ but here I renamed it to ´name´ but I forgot to overwrite that particular ´text´ in that piece of code. But my problem is still current. Commented Oct 28, 2015 at 21:17
  • Is it possible to explain what does 'pretty' refers to in your url request ? Commented Jul 30, 2019 at 8:51
  • @EmnaJaoua elastic.co/guide/en/elasticsearch/reference/current/… Commented Jul 30, 2019 at 9:10
  • Thanks for your reply. I am trying to do the same also using HttpClient library (since I need to work with .Net Client 2.2 framework). I am defining the url and credentials through client.BaseAddress and client.DefaultRequestHeaders.Authorization but when I get the response through response = await client.GetAsync(path), the response is not successful so I was not able to make a request. I am using exactly the same query format as yours (string s). Any useful links for help please ? Commented Jul 30, 2019 at 9:33

1 Answer 1

2

Final solution for my question is in this code

string url_req = "http://localhost:9200/my_index/my_type/_search?pretty";

string s = "{\"query\": {\"match\": { \"name\" : \"john\" }}}";

using (var client = new WebClient())
{
    Console.Write(client.UploadString(url_req, "POST", s));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer used a POST verb. For security reasons, I want to find a solution with the GET verb. GNU/Linux cURL will allow you to send message bodies with the GET verb so I don't understand why so many other WebRequest tools don't allow it. From what I've read elsewhere on SO, sending a request body with a GET request is "weird", but the standard doesn't specifically disallow it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.