-1

I am getting error cannot send a content-body with this verb-type. I am calling a GET Endpoint from a C# VSTO desktop application. What am I doing wrong.

public static string GetCentralPath(LicenseMachineValidateRequestDTO licenseMachine)
{
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization =    new AuthenticationHeaderValue("Bearer", Properties.Settings.Default.Properties["JWT"].DefaultValue.ToString());
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri($"{Constants.URL.APIBase}licensemachine/GetCentralPath"),
            Content = new StringContent(JsonConvert.SerializeObject(licenseMachine), Encoding.UTF8, "application/json"),
        };
        
        using (HttpResponseMessage response = client.SendAsync(request).GetAwaiter().GetResult()) // Causing ERROR
        {
            var result = GetStringResultFromHttpResponseMessage(response, true);
            if (string.IsNullOrEmpty(result))
                return null;
            return JsonConvert.DeserializeObject<string>(result);
        }
    }
}

The end point looks like the following:

[HttpGet("GetCentralPath")]
public async Task<IActionResult> GetCentralPath(LicenseMachineValidateRequestDTO dto)
{
    // Some code
}
Sujoy
  • 1,051
  • 13
  • 26
  • 1
    The error says you can't define a content body for the HttpRequestMessage with Method as Get. If content is required then use HttpMethod Post (also changing the definition on the controller) – Matt Feb 22 '22 at 17:37
  • There has to be a proper way to accomplish this. Why should API layer be impacted due to some buggy client behavior. What if I am invoking a third party API, should I request the vendor to make changes. – Sujoy Feb 22 '22 at 17:45
  • Change GET to PUT. GET is used when you receive a request on server or when you receive a response on client. You are sending on client and need to use POST. – jdweng Feb 22 '22 at 17:48
  • 1
    This is called out in the actual HTTP Spec. Even if you manage to send a (malformed) GET with a body, no server is required to honor it. If there is any API that requires a body on a GET, the API is out of spec, not the client. – Bradley Uffner Feb 22 '22 at 17:51
  • @Sujoy its not a buggy client, that's how all Http requests work and have done for a very long time. If you want to send data via a GET request it needs to be in the form of route data, query string or headers but not body content – Matt Feb 23 '22 at 09:20

1 Answers1

-1

fix the action, you cannot send body data with get, see this post HTTP GET with request body

[HttpPost("GetCentralPath")]
public async Task<IActionResult> GetCentralPath(LicenseMachineValidateRequestDTO dto)

and fix request , replace Method = HttpMethod.Get with Post, this is what generates an error

var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri($"{Constants.URL.APIBase}licensemachine/GetCentralPath"),
            Content = new StringContent(JsonConvert.SerializeObject(licenseMachine), Encoding.UTF8, "application/json"),
        };
Serge
  • 40,935
  • 4
  • 18
  • 45
  • We can always send a body data with GET and the endpoint is working fine. I tested it with swagger. The problem is with the client. – Sujoy Feb 23 '22 at 03:48
  • Swagger is probably assuming that the `LicenseMachineValidateRequest` is bound from a query string because it's impossible to come from the body. – Matt Feb 23 '22 at 09:18
  • @Sujoy Why are you posting the questions, if you are not going to follow the answers? People are waisting time because of you. Just common sense should tell you that it is impossible, since a compiller generates a runtime error. – Serge Feb 23 '22 at 14:07