Currently to send a parameterized GET request to an API interface I am writing the following code:
api/master/city/filter?cityid=1&citycode='ny'
But I see that there is a limit on the URL length of 2,083 characters.
To avoid this I would like to send the parameters in json format in the content body for a GET request.
However, I see that none of the Get methods for the HttpClient allow for a content body to be sent. For the POST I could see there is a method within HttpClient named PostAsync that allows for a content body.
Is there a way to send parameters for a GET request not in the URL in order to avoid the URL length limit?
Please read the caveats at the end of this answer as to why HTTP GET requests with bodies are, in general, not advised.
If you are using .NET Core, the standard HttpClient
can do this out-of-the-box. For example, to send a GET request with a JSON body:
HttpClient client = ...
...
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);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
.NET Framework doesn't support this out-of-the-box (you will receive a ProtocolViolationException
if you try the above code). Thankfully Microsoft has provided the System.Net.Http.WinHttpHandler package that does support the functionality - simply install and use it instead of the default HttpClientHandler
when constructing your HttpClient
instances:
var handler = new WinHttpHandler();
var client = new HttpClient(handler);
<rest of code as above>
Reference: https://github.com/dotnet/corefx/issues/28135#issuecomment-467261945
Caveats: