简体   繁体   English

无法在 RestSharp 107.1.2 中接收发布请求的正文

[英]Unable to receive body of a post request in RestSharp 107.1.2

I've just updated my RestSharp application to 107.1.2 and are now unable to send post request.我刚刚将我的 RestSharp 应用程序更新到 107.1.2,现在无法发送发布请求。 To test the problem I've set up a small express.js webserver to receive the requests from my C# application.为了测试这个问题,我设置了一个小型 express.js 网络服务器来接收来自我的 C# 应用程序的请求。 On every post request I made, there is always no body available.在我提出的每个帖子请求中,总是没有可用的正文。

RestClientOptions options = new RestClientOptions("http://127.0.0.1:3000");
RestClient client = new RestClient(options);
RestRequest request = new RestRequest("api/test");
request.AddParameter("test1", "test1");
request.AddParameter("test2", "test2");
request.AddParameter("test3", "test3");
TestResponse response = await client.PostAsync<TestResponse>(request);

The code is based on the QueryString documentation https://restsharp.dev/usage.html#query-string .该代码基于 QueryString 文档https://restsharp.dev/usage.html#query-string And yes, the params from the request I receive on the webserver are also empty.是的,我在网络服务器上收到的请求的参数也是空的。 To test plain post request, I even tried the following:为了测试普通的 post 请求,我什至尝试了以下方法:

RestClientOptions options = new RestClientOptions("http://127.0.0.1:3000");
RestClient client = new RestClient(options);
RestRequest request = new RestRequest("api/test");

const string json = "{ data: { foo: \"bar\" } }";
request.AddStringBody(json, ContentType.Json);
TestResponse response = await client.PostAsync<TestResponse>(request);

I can't find the problem why the requests are sent without an actual post body.我找不到为什么在没有实际帖子正文的情况下发送请求的问题。 In the previous version I've updated from, everything works fine.在我更新的上一个版本中,一切正常。

Hope you can help me.希望您能够帮助我。

Can you please Try this:你能试试这个:

request.RequestFormat = DataFormat.Json;

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

or或者

const string json = "{ data: { foo: \"bar\" } }";
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
RestClientOptions options = new RestClientOptions("http://127.0.0.1:3000");
var client = new RestSharp.RestClient(options);
var request = new RestSharp.RestRequest(RestSharp.Method.POST);
request.RequestFormat = RestSharp.DataFormat.Json;
request.AddHeader("Content-Type", "application/json");
const string json = "{ data: { foo: \"bar\" } }";
request.AddBody(JsonConvert.SerializeObject(json));
var response = client.Execute(request);

Try this please请试试这个

       RestClientOptions options = new RestClientOptions("http://127.0.0.1:3000");
       var restClient = new RestClient(options);

        var restRequest = new RestRequest(Method.POST)
        {
            RequestFormat = DataFormat.Json
        };

        const string json = "{ data: { foo: \"bar\" } }";
        request.AddBody(JsonConvert.SerializeObject(json));

        var result = restClient.Post<TResponse>(restRequest);

        if (!result.IsSuccessful)
        {
            throw new HttpException($"Item not found: {result.ErrorMessage}");
        }

        return result.Data;

I am having trouble understanding why do you think the request is made without the body.我无法理解您为什么认为请求是在没有正文的情况下提出的。 I would expect that the express API gives you no body, but it doesn't mean the request has no body.我希望快递 API 没有给你任何正文,但这并不意味着请求没有正文。

I traced your requests, and you can see them here .我跟踪了您的请求,您可以在此处查看

The first snippet produces the following request:第一个片段产生以下请求: 在此处输入图像描述

As you can see it has the correct content type and the body is correctly formed as form URL encoded body content.如您所见,它具有正确的内容类型,并且正文正确形成为 URL 编码的正文内容。

Your second snippet also produces the correct request with JSON body:您的第二个片段还使用 JSON 主体生成正确的请求:

在此处输入图像描述

It is impossible to say why your server doesn't understand these requests, as you haven't specified what the server needs, but I don't see any issues with those requests per se.不可能说你的服务器为什么不理解这些请求,因为你没有指定服务器需要什么,但我看不出这些请求本身有任何问题。

If you you want to add parameters in the request use below way:如果要在请求中添加参数,请使用以下方式:

RestClient client = new("http://127.0.0.1:3000");
RestRequest restRequest = new("api/test");
restRequest.AddParameter("test1", "test1");
request.AddParameter("test2", "test2");
request.AddParameter("test3", "test3");
RestResponse response = client.ExecuteAsync(restRequest,Method.Post).Result;      
Assert.AreEqual(HttpStatusCode.OK, response);
 

If you you want to add json body in the request use the below way:如果您想在请求中添加 json 正文,请使用以下方式:

RestClient client = new("http://127.0.0.1:3000");
RestRequest restRequest = new("api/test");
string requestBody=  "{\"data\":{\"foo\":\"bar\"}}"
restRequest.AddStringBody(requestBody, DataFormat.Json);
RestResponse response = client.ExecuteAsync(restRequest, Method.Post).Result;
Assert.AreEqual(HttpStatusCode.OK, response);

 

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

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