2

I require to send a HTTP GET request with a content body. Yes I know this is frowned upon but it is technically a legal transaction.

Currently the .NET WebRequest class will fail when attempting to get the request stream as shown below.

string url = "https://someplace.com/api";
var wr = WebRequest.Create(url);
wr.Method = "GET";

string json = JsonConvert.SerializeObject(query);
byte[] byteData = Encoding.UTF8.GetBytes(json);

using (var req = wr.GetRequestStream()) // Exception thrown here
{
  req.Write(byteData, 0, byteData.Length);
  req.Flush();
}

What other options do I have?

The equivalent CURL command is:

curl -XGET 'localhost:9200/twitter/tweet/_search?pretty' -d'
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}'
fungus1487
  • 1,760
  • 2
  • 20
  • 39
  • You might want to read [this answer](http://stackoverflow.com/a/983458/22099) where it is shown that it is not a "legal transaction". On the other hand, if you insist, you can just go low level and establish connection yourself and send the bytes. – miha Nov 26 '16 at 23:07
  • @miha I don't see where it says it is required to *not* send a body. I Agree that it should be meaningless however the ElasticSearch API uses it to define complex queries which would be serialised into very long URL's. The point is it doesn't say that it can't just that it should be meaningless. – fungus1487 Nov 26 '16 at 23:55
  • It is not required but it's not [recommended](https://tools.ietf.org/html/rfc2616#section-4.3) to use. The reason for this is that your request might have multiple layers (proxy,load balancer) in between. A GET request can be cached on the way and body will be ignored. You might get bugs in production if you are not controlling the whole stack. – Filip Cordas Nov 28 '16 at 16:21

1 Answers1

-2

you can send only query strings using HTTP GET (the curl command does the same, you can inspect it with some tools like fiddler). you will have to pass the entire string like below :

string json = JsonConvert.SerializeObject(query);
string url = "https://someplace.com/api?pretty="+json;
var wr = WebRequest.Create(url);
wr.Method = "GET";

note: curl -XGET & curl -G are same.

Vilsad P P
  • 1,529
  • 14
  • 23