23

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?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
leninyee
  • 346
  • 1
  • 2
  • 7

2 Answers2

50

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); 
Der_Meister
  • 4,771
  • 2
  • 46
  • 53
  • 5
    Worth noting that ParameterType.QueryString was added in RestSharp v104.3. My project referenced an older version so this was unavailable without upgrading. – John Hargrove Feb 24 '15 at 03:37
  • 3
    v105.0.0 introduced `request.AddQueryParameter(name,value)` which is a wrapper around `request.AddParameter(name, value, ParameterType.QueryString)` – Simon Elms Feb 26 '17 at 05:38
29

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.

Ender2050
  • 6,912
  • 12
  • 51
  • 55
  • 3
    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. – Sebastian Zaklada Aug 13 '12 at 12:51
  • It works, but as @SebastianZaklada says. It isn't that really a "nice" solution. – wouterds Aug 01 '13 at 09:38
  • 4
    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... – Gutemberg Ribeiro Sep 28 '13 at 16:22
  • protected T Execute(RestRequest request) where T : new() { if (!string.IsNullOrWhiteSpace (m_token)) { request.AddParameter ("token", m_token); } var tcs = new TaskCompletionSource (); m_client.ExecuteAsync (request, response => { tcs.SetResult (response.Data); }); tcs.Task.Wait (); return tcs.Task.Result; } – Gutemberg Ribeiro Sep 28 '13 at 16:24
  • @SebastianZaklada, use ParameterType.QueryString. – Der_Meister Mar 23 '14 at 10:29
  • 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}"` – QueueHammer Jan 04 '17 at 22:18
  • 1
    @QueueHammer, are you sure that C# 6 string interpolation will handle ?&= symbols inside values correctly (URL encoding)? – Der_Meister Feb 27 '17 at 06:15
  • should you really be putting the token in the url? – BenKoshy Mar 04 '18 at 10:50