简体   繁体   English

如何使用请求正文进行 GET

[英]How to make GET with request body

How do I make a GET query with request body using RestSharp (I'm using RestSharp v. 106.6.10 with .NET Core 2.2 ).如何使用RestSharp使用请求正文进行GET查询(我使用的是 RestSharp v. 106.6.10.NET Core 2.2 )。 I can do it using WebClient / Postman etc. no problem.我可以使用WebClient / Postman等来完成,没问题。

Here's the code failing with {["A non-empty request body is required."]} .这是以{["A non-empty request body is required."]}失败的代码。

var client = new RestClient("BaseUri");
var request = new RestRequest("URL", Method.GET);
request.AddJsonBody(Body); 
client.Execute(request); // {["A non-empty request body is required."]}

This represents a valid use case, pity if it's not supported.这代表了一个有效的用例,遗憾的是它不受支持。

Update : The motivation for having GET requests with body is to avail of get requests having complex parameters, which can't be nicely encoded into a query string.更新:使用带有正文的 GET 请求的动机是利用具有复杂参数的 get 请求,这些参数无法很好地编码到查询字符串中。 I know people serialize their jsons an put them into a querystrings but I'd rather put it into a request body, considering it's a permissible usage after all.我知道人们将他们的 json 序列化并将它们放入查询字符串中,但我宁愿将其放入请求正文中,毕竟这是允许的用法。

From my experience AddJsonBody is completely broken (had a multiple times when it wasn't serialize my model just pasting in Body something like MyProject.Models.MyModel or even left body empty).根据我的经验, AddJsonBody完全损坏(多次没有序列化我的模型,只是在 Body 中粘贴 MyProject.Models.MyModel 之类的东西,甚至将正文留空)。 So I use following:所以我使用以下内容:

var client = new RestClient("BaseUri");
var request = new RestRequest("URL", Method.GET);
request.AddHeader("Content-Type", "application/json");
string serializedBody = Newtonsoft.Json.JsonConvert.SerializeObject(Body);
request.AddParameter("application/json; charset=utf-8", serializedBody, ParameterType.RequestBody);
client.Execute(request);

UPDATE sorry i wasn't patient when reading you question.更新抱歉,我在阅读您的问题时没有耐心。 Answer is provided for RestSharp not PostSharpRestSharp而非PostSharp提供答案

I solved it by using reflection , to trick WebRequest that it is legal to send a body with a GET request.我通过使用反射解决了这个问题,以欺骗WebRequest发送带有 GET 请求的主体是合法的。

1.Create Model for body parameters, 1.为身体参数创建模型,

public class SampleBodyModel
{
    public String name{ get; set; }
    public String password{ get; set; }
}
  1. Initialize the request.初始化请求。

     SampleBodyModel sampleRequest = new SampleBodyModel { name= "name", password= "password" }; //initialize the API call var request = WebRequest.Create("API_END_POINT"); request.ContentType = "application/json"; request.Method = "GET"; var myHttpWebRequest = (HttpWebRequest)request; // ------- Add these two lines if your using JWT token ------- myHttpWebRequest.PreAuthenticate = true; myHttpWebRequest.Headers.Add("Authorization", "Bearer " + "TOKEN"); // ----------------------------------------------------------- var type = myHttpWebRequest.GetType(); var currentMethod = type.GetProperty("CurrentMethod", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(myHttpWebRequest); var methodType = currentMethod.GetType(); methodType.GetField("ContentBodyNotAllowed", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentMethod, false); //Add the Request body using (var streamWriter = new StreamWriter(myHttpWebRequest.GetRequestStream())) { streamWriter.Write(Newtonsoft.Json.JsonConvert.SerializeObject(sampleRequest)); } var response = (HttpWebResponse)myHttpWebRequest.GetResponse(); var responseStream = response.GetResponseStream(); var myStreamReader = new StreamReader(responseStream, Encoding.Default); var json = myStreamReader.ReadToEnd(); responseStream.Close(); response.Close();

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

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