English 中文(简体)
使用 RestSharp 添加 GGet 参数到 POST 请求中的 GGet 参数
原标题:Add a GET parameter to a POST request with RestSharp

我要向像这样的 URL 提出 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?

最佳回答

如果您在资源 URL 上添加标记, 并且 (2) 指定参数Type. UrlSection 像这样 :

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

这远非理想 但我找到的最简单的方法... 仍然希望找到更好的方法

问题回答

目前版本的SepSharp使用一个短方法使用模板:

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签