简体   繁体   English

RestSharp匹配的Curl语法

[英]RestSharp matching Curl syntax

I'm trying to talk to an API, which seems to be using a non-standard setup, here's the curl request that works: 我正在尝试与似乎正在使用非标准设置的API进行通讯,这是有效的curl请求:

curl -d '{"id" : "blah"}' -H "Content-Type: application/json" -X GET http://127.0.0.1:2796/api/0.8/testing/testme

This returns what I expect, however whenever I try to do this using RestSharp, the API returns an error stating that it received no json. 这返回了我期望的结果,但是,每当我尝试使用RestSharp进行此操作时,API都会返回一个错误,指出它没有收到json。 Here's the code I'm trying to use. 这是我要使用的代码。

var client = new RestClient(server);
var request = new RestRequest("/api/0.8/testing/testme", Method.GET);

request.RequestFormat = DataFormat.Json;
request.AddJsonBody(new {id = "blah"});

IRestResponse response = client.Execute(request);
var content = response.
Console.WriteLine("Response: " + content);

It's a GET request, but it needs to also send json data, so I'm bit confused as to how that works. 这是一个GET请求,但它还需要发送json数据,因此我对它的工作方式有些困惑。 Does it put it in the body, or in the URL? 是否将其放在正文中或URL中?

Any advice or input would be greatly appreciated. 任何建议或意见将不胜感激。

Thanks 谢谢

Why are you posting JSON in a get request? 为什么要在get请求中发布JSON? That doesn't make sense. 那没有道理。

Are you sure you don't mean that you are posting JSON and then receiving a JSON response? 您确定不是要发布JSON,然后收到JSON响应吗?

My suggestion is to download a tool called "Postman" and try to get it working there first. 我的建议是下载一个名为“邮递员”的工具,然后尝试使其首先在那里工作。

Try this: 尝试这个:

var client = new RestClient("http://127.0.0.1:2796/api/0.8/testing/testme");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", "{\"id\" : \"blah\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

Then you can read the response and interpret that into your project. 然后,您可以阅读响应并将其解释为您的项目。

Using 使用

request.AddParameter("","",ParameterType.RequestBody);

instead of 代替

AddJsonBody

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

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