我想对这样的网址发出POST请求:
http://localhost/resource?auth_token=1234
我想在体内发送JSON。我的代码看起来像这样:
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);
如何将auth_token
参数设置为GET参数并将请求设为POST?
答案 0 :(得分:36)
当前版本的RestSharp有一个使用模板的简短方法:
var request = new RestRequest("resource?auth_token={token}", Method.POST);
request.AddUrlSegment("token", "1234");
或者,您可以添加不带模板的参数:
var request = new RestRequest("resource", Method.POST);
request.AddQueryParameter("auth_token", "1234);
或
var request = new RestRequest("resource", Method.POST);
request.AddParameter("auth_token", "1234", ParameterType.QueryString);
答案 1 :(得分:23)
如果您1)将令牌添加到资源URL并且2)指定ParameterType.UrlSegment,这应该有效:
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);
这远非理想 - 但我发现最简单的方式......仍然希望找到更好的方法。