Reputation: 31
I am trying to make a GET request with a body to a REST API - without success. The API sits on top of Elastisearch, which requires a body for requests. The body contains the filters and requests for specific fields for the request. I have tried multiple different requests in Postman and they work fine. The body is forwarded and the results are filtered as expected.
The API is state run which means i have no control over it what so ever. It refuses any request that is not GET (POST, PUT etc.).
I have tried with RestSharp, but it refuses to make a GET request with a body. Error message is: "Http verb GET does not support body". (i tried to get the C# code from Postman, but it throws the same exception).
I have also tried with HttpClient, HttpWebRequest and WebClient. Same error in all of them. To try a new platform i created a PowerShell script to run from C#, but it throws the same exception.
Is this just impossible to do in C#?
My only option now is to create e.g. a PHP (or other language) script that can query the API succefully, and then access this script from C#. But it seems like quite a workaround only because of the GET/body problem.
Any advice or ressources you could provide or refer me to would be greatly appreciated! Thank you.
Btw. i have seen a lot of posts on Stackoverflow on this problem. Many of them are quite old so i am hoping that perhaps something has changed. Also many of them end up with a solution where you use POST instead of GET - which is not a option for me.
Upvotes: 1
Views: 6184
Reputation: 93
Using RestSharp, in the request.AddParameter
use ParameterType.GetOrPost
instead of ParameterType.RequestBody
request.AddParameter("application/json", "things", ParameterType.GetOrPost);
Upvotes: 0
Reputation: 327
You can use NEST (https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest.html) - client for elastic for .net
Example code:
var elasticUrls = new Uri[]{new Uri("http://localhost:9200") };
var connect = new ConnectionSettings(
new SniffingConnectionPool(elasticUrls, true, null),
(builtin, settings) =>
new JsonNetSerializer(
builtin,
settings,
null,
null,
new JsonConverter[] {new StringEnumConverter()})
);
var client = new ElasticClient(connect);
var searchResult = await client.SearchAsync<ElasticModel>(
d => d
.Query(q => q.Bool(qd => qd
.Must(m => m.MatchAll())
))
.From(0)
.Size(10)
).ConfigureAwait(false);
/// <summary>
/// Some model from Elastic
/// </summary>
class ElasticModel
{
public int Id { get; set; }
}
Upvotes: 1