简体   繁体   English

使用 RestSharp 将 Curl 转换为 C#

[英]Converting Curl to C# with RestSharp

I'm trying to convert this curl command to C# using RestSharp, and I'm having problems specifically with the data parameter (token and uri variables have been replaced w/dummy values for this example):我正在尝试使用 RestSharp 将此 curl 命令转换为 C#,并且我在数据参数方面遇到了问题(此示例中,令牌和 uri 变量已被替换为虚拟值):

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'UserToken: usertoken' --header 'AppToken: apptoken' -d '[{"individualRecordNumber":"foo","score":"bar"}]' 'apiEndpoint'

I've been able to successfully do several GET requests within this same API, so I'm good with the overall request format and headers, I'm just having issues figuring out how to format the data and where to append it.我已经能够在同一个 API 中成功地执行几个 GET 请求,所以我对整体请求格式和标头很好,我只是在弄清楚如何格式化数据以及 append 它的位置时遇到问题。 Here's the rest of the request without the data:这是没有数据的请求的 rest:

var client = new RestClient(apiEndpoint);
var request = new RestRequest(Method.POST);
request.AddHeader("usertoken", usertoken);
request.AddHeader("apptoken", apptoken);
request.AddHeader("content-type", "application/json");
    
IRestResponse response = client.Execute(request);

I've tried using both AddParameter and AddJsonBody with various combinations of serialized and unserialized versions of the data.我尝试将 AddParameter 和 AddJsonBody 与数据的序列化和非序列化版本的各种组合一起使用。

Example of building data directly as string:将数据直接构建为字符串的示例:

string examInfo = @"[{""individualRecordNumber"":""foo"",""score"":""bar""}]";

Example of building data as object:构建数据为 object 的示例:

object[] arrayObj = new object[1];
arrayObj[0] = new { individualRecordNumber = "foo", score = "bar" };

This is for a project with an extremely tight turnaround, so any help is much appreciated!这是一个周转时间非常紧迫的项目,因此非常感谢任何帮助!

If you need a Post, to "wire in" the Post-Body, AddParameter has this overload如果你需要一个 Post 来“连接” Post-Body,AddParameter 有这个重载

request.AddParameter("application/json", "mybody", ParameterType.RequestBody);

or, in your case或者,在你的情况下

request.AddParameter("application/json", examInfo, ParameterType.RequestBody);

but maybe better (if you have later version)但也许更好(如果你有更高版本)

request.AddJsonBody(new { A = "foo", B = "bar" });

And the initial constructor: (which you seem to have, but making sure to note it for future readers)和最初的构造函数:(你似乎有,但一定要为未来的读者注意它)

var request = new RestRequest(Method.POST);

More full example:更完整的例子:

var client = new RestClient("https:blahblahblah");
var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/json");

/* option1 */
request.AddParameter("application/json", "{mybody:'yes'}", ParameterType.RequestBody);

/* OR option 2 */
request.AddJsonBody(new { A = "foo", B = "bar" });

IRestResponse response = client.Execute(request);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM