添加GET参数与RestSharp POST请求参数、GET、POST、RestSharp

2023-09-03 02:25:24 作者:专属总裁

我想打一个POST请求,像这样的网址:

I want to make a POST request to a URL like this:

http://localhost/resource?auth_token=1234

和我要在身体发出JSON。我的code看起来是这样的:

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);

我如何设置的auth_token 参数是一个GET参数,并请求为POST?

How can I set the auth_token parameter to be a GET parameter and make the request as POST?

推荐答案

这应该工作,如果你1)令牌添加到资源的URL和2)指定ParameterType.UrlSegment是这样的:

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.

 
精彩推荐
图片推荐