I want to make a POST request to a URL like this:
http://localhost/resource?auth_token=1234
And I want to send JSON in the body. My code looks something like this:
var client = new RestClient("http://localhost"); var request = new RestRequest("resource", Method.POST); request.AddParameter("auth_token", "1234"); request.AddBody(json); var response = client.Execute(request);
How can I set the auth_token
parameter to be a GET parameter and make the request as POST?
var client = new RestClient("http://localhost"); var request = new RestRequest("resource", Method. POST); request. AddParameter("auth_token", "1234"); request. AddBody(json); var response = client.
Since RestSharp uses the HttpClient, we should consider similar issues regarding the RestClient instantiation.
The current version of RestSharp has a short method that makes use of a template:
var request = new RestRequest("resource?auth_token={token}", Method.POST); request.AddUrlSegment("token", "1234");
Alternatively, you can add a parameter without a template:
var request = new RestRequest("resource", Method.POST); request.AddQueryParameter("auth_token", "1234);
or
var request = new RestRequest("resource", Method.POST); request.AddParameter("auth_token", "1234", ParameterType.QueryString);
This should work if you 1) add the token to the resource url and 2) specify ParameterType.UrlSegment like this:
var client = new RestClient("http://localhost"); var request = new RestRequest("resource?auth_token={authToken}", Method.POST); request.AddParameter("auth_token", "1234", ParameterType.UrlSegment); request.AddBody(json); var response = client.Execute(request);
This is far from ideal - but the simplest way I've found... still hoping to find a better way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With