Add a GET parameter to a POST request with RestSharp
Asked Answered
C

2

24

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?

Crocodile answered 25/5, 2012 at 1:6 Comment(0)
M
30

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.

Marvellamarvellous answered 8/6, 2012 at 0:48 Comment(8)
Isn't there a nicer solution, which does not involve using UrlSegment in a manually specified query string token? This works, but it is a nasty workaround... and it does not fit in well in all situations.Aplanatic
It works, but as @SebastianZaklada says. It isn't that really a "nice" solution.Diplex
Does anybody get it working without the UrlSegment? It is terrible... We need be able to add parameter in the URL regardless of the HTTP VERB, since it is possible following the HTTP specifications... In my case, I have a base class where all the requests pass over it, and subclasses that get send only the post data, so I want encapsulate this token inside the base class otherwise, every time I need the token I need to add it in the URL... for example this is the method I call on base class It dont work, unless I previously set the placeholder on the request... Shame...Langlois
protected T Execute<T>(RestRequest request) where T : new() { if (!string.IsNullOrWhiteSpace (m_token)) { request.AddParameter ("token", m_token); } var tcs = new TaskCompletionSource<T> (); m_client.ExecuteAsync<T> (request, response => { tcs.SetResult (response.Data); }); tcs.Task.Wait (); return tcs.Task.Result; }Langlois
@SebastianZaklada, use ParameterType.QueryString.Right
I started using string interpolation in C# 6 and the syntax is the same with out the extra line of code. $"resource?auth_token={authToken}"Kulsrud
@QueueHammer, are you sure that C# 6 string interpolation will handle ?&= symbols inside values correctly (URL encoding)?Right
should you really be putting the token in the url?Distich
R
51

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); 
Right answered 23/3, 2014 at 10:24 Comment(2)
Worth noting that ParameterType.QueryString was added in RestSharp v104.3. My project referenced an older version so this was unavailable without upgrading.Karena
v105.0.0 introduced request.AddQueryParameter(name,value) which is a wrapper around request.AddParameter(name, value, ParameterType.QueryString)Interplead
M
30

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.

Marvellamarvellous answered 8/6, 2012 at 0:48 Comment(8)
Isn't there a nicer solution, which does not involve using UrlSegment in a manually specified query string token? This works, but it is a nasty workaround... and it does not fit in well in all situations.Aplanatic
It works, but as @SebastianZaklada says. It isn't that really a "nice" solution.Diplex
Does anybody get it working without the UrlSegment? It is terrible... We need be able to add parameter in the URL regardless of the HTTP VERB, since it is possible following the HTTP specifications... In my case, I have a base class where all the requests pass over it, and subclasses that get send only the post data, so I want encapsulate this token inside the base class otherwise, every time I need the token I need to add it in the URL... for example this is the method I call on base class It dont work, unless I previously set the placeholder on the request... Shame...Langlois
protected T Execute<T>(RestRequest request) where T : new() { if (!string.IsNullOrWhiteSpace (m_token)) { request.AddParameter ("token", m_token); } var tcs = new TaskCompletionSource<T> (); m_client.ExecuteAsync<T> (request, response => { tcs.SetResult (response.Data); }); tcs.Task.Wait (); return tcs.Task.Result; }Langlois
@SebastianZaklada, use ParameterType.QueryString.Right
I started using string interpolation in C# 6 and the syntax is the same with out the extra line of code. $"resource?auth_token={authToken}"Kulsrud
@QueueHammer, are you sure that C# 6 string interpolation will handle ?&= symbols inside values correctly (URL encoding)?Right
should you really be putting the token in the url?Distich

© 2022 - 2024 — McMap. All rights reserved.