0

I want to send an HTTP GET with a request body. I know there is much heated debate about whether this should ever be done or not but I am not interested in debating it I simply want to do it. I'm using C# and ASP.NET and my code is below. Unfortunately it throws an exception "Cannot send a content-body with this verb type". Please, any help on how to get this done will be very appreciated!

// Serialize our concrete class into a JSON String
            var stringPayload = JsonConvert.SerializeObject(memRequest);

            // Wrap our JSON inside a StringContent which then can be used by the HttpClient class
            var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");

            using (var httpClient = new HttpClient())
            {
                HttpRequestMessage request = new HttpRequestMessage
                {
                    Method = HttpMethod.Get,
                    RequestUri = u,
                    Content = httpContent
                };

                var result = httpClient.SendAsync(request).Result;
                result.EnsureSuccessStatusCode();

                var responseBody = result.Content.ReadAsStringAsync().ConfigureAwait(false);
PeteyMFT
  • 75
  • 2
  • 8
  • 2
    As the error is trying to tell you, you can't do that. – SLaks Nov 29 '18 at 23:39
  • Possible duplicate of [Payload body of a HTTP GET request](https://stackoverflow.com/questions/51250018/payload-body-of-a-http-get-request). what debate are you referring to? GET never had a "request body" specified. related: https://stackoverflow.com/a/50589852/1132334 – Cee McSharpface Nov 29 '18 at 23:43
  • 1
    I think your issue is that the .NET team did debate it and decided that GET requests shouldn't have bodies. Since the rest of the world is able to get by with this restriction, you might want to rethink your approach. – snow_FFFFFF Nov 29 '18 at 23:45
  • @dlatikay thanks for sending but that is specific to postman, I'm working with C# and ASP.NET – PeteyMFT Nov 29 '18 at 23:52
  • Apparently this can be done in .NET Core https://stackoverflow.com/a/47902348/62600 – Todd Menier Dec 01 '18 at 17:00
  • Thank you @ToddMenier. This is helpful and has lots of interesting additional information. – PeteyMFT Dec 03 '18 at 18:47
  • Possible duplicate of [Possible for HttpClient to send content or body for GET request?](https://stackoverflow.com/questions/43421126/possible-for-httpclient-to-send-content-or-body-for-get-request) – Ian Kemp May 09 '19 at 12:02

0 Answers0