Can't send content body with GET request

I am trying to do a simple "request body search" on Elasticsearch like in the following example , but instead of .NET instead of curl

$ curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
'

      

Below is my .NET code.

var uri = "http://localhost:9200/myindex/_search";
var json = "{ \"query\" : { \"term\" : { \"user\" : \"kimchy\" } } }";

var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
request.ContentType = "text/json";
request.Method = "GET";

var responseString = string.Empty;

using (var streamWriter = new System.IO.StreamWriter(request.GetRequestStream()))
{
    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();

    var response = (System.Net.HttpWebResponse)request.GetResponse();
    using (var streamReader = new System.IO.StreamReader(response.GetResponseStream()))
    {
        responseString = streamReader.ReadToEnd();
    }
}

      

However, I am getting the following error.

Cannot send a content-body with this verb-type.
...
Exception Details: System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
...
Line 54: using (var streamWriter = new System.IO.StreamWriter(request.GetRequestStream()))

      

Is there a way to send a content body with a request GET

using standard .NET classes. Or is there a workaround?

+3


source to share


1 answer


Changing Method

to POST

is a workaround.

request.Method = "POST";

      



MSDN states that it will be called ProtocolViolationException

if the method GetResponseStream()

is called with GET

or HEAD

.

+3


source







All Articles