0

Is there any way at all that I can send a GET request with a JSON body using c#? I am making a call to an API to retrieve a list of items using a GET request, but I have to pass the customer_id in JSON. I am able to do this successfully in Postman and Python. However, the legacy app that I am working with is built as c# .NET winform. I am able to make other calls using HttpClient, but after some research I am finding that this class does not allow GET request with body except when using CORE. Are there any other alternatives?

Panthr073
  • 13
  • 4
  • Can you talk us through _why_ you want to do that? It is unusual to pass a body with a GET request. – mjwills Apr 03 '20 at 02:41
  • @mjwills the api that I am using request that the customer_id is sent via the body in json format. – Panthr073 Apr 03 '20 at 02:52
  • Have you tried passing it in the querystring? Why API is this? This is quite unusual. – mjwills Apr 03 '20 at 02:53
  • 1
    Check if the API also supports `POST` verb on the same call.The only API I used supporting `GET` with a body is ElasticSearch, which they also provide an option to run the same method with `POST`. – weichch Apr 03 '20 at 03:37

2 Answers2

0

According to Ian Kemp's answer to this question, This can be done in .NET Framework projects using the System.Net.Http.WinHttpHandler Library. (I'll just add the relevant part of the answer here, but I recommend to go check his full answer)

First, Install the System.Net.Http.WinHttpHandler Library from Nuget and then use it as your http client handler as described below:

var handler = new WinHttpHandler();
var client = new HttpClient(handler);
var request = new HttpRequestMessage
{
    Method = HttpMethod.Get,
    RequestUri = new Uri("some url"),
    Content = new StringContent("some json", Encoding.UTF8, ContentType.Json),
};

var response = await client.SendAsync(request).ConfigureAwait(false);
//Handle the response as you see fit
-1

With the HTTP method GET, the body has no meaning. It will be ignored according to the HTTP specs. When getting resources from your API using the GET http verb, you have the option to pass a query string (http://somedomain.com/api/getValues?key=1) or pass the parameter directly in the url (http://somedomain.com/api/getValues/1)

To receive it in your controller, you would have to expect either the parameter or the query string like this:

If the parameter is in the URL:

[HttpGet("/api/getValues/{id}")]
public async Task<IActionResult> GetValues(int id){}

(Make sure that your parameter name in the function matches the name that you gave to it in the route)

If the parameter is a query string:

[HttpGet("/api/getValues")]
public async Task<IActionResult> GetValues(string key){}

(Make sure that the query string key name matches the parameter name in your function)

The best practice is to use the parameter in the URL. Query strings are very useful, but you have to know when to use it, for example, if you want to narrow down the results given certain values, you could the query string to send them.

  • `The HTTP method GET does not allow passing a body.` This is not true. https://stackoverflow.com/questions/978061/http-get-with-request-body – mjwills Apr 03 '20 at 08:06
  • Referencing the same thread, it says that "Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request". Read through before making any argument :) – Kenny Perroni Apr 03 '20 at 13:49