RestSharp 从正文中删除点

RestSharp removing dot from body

我正在使用 restsharp 向我的端点执行 POST 请求。

当我添加正文时,我这样做:

request.AddParameter("text/json", message, ParameterType.RequestBody);

字符串消息是这样完成的:VALUE1.VALUE2 真的很简单。

但我的端点只收到 VALUE1

端点签名是:

[HttpPost]
public HttpResponseMessage DoJob([FromBody] string id)

你知道为什么吗?我是否必须以某种方式对我发送的消息进行编码?

出于测试目的对邮递员进行同样的操作我没有遇到这种行为。

谢谢!

这是我的 RestSharp 版本 105.1.0.0 的工作示例:

var message = "VALUE1.VALUE2"
var client = new RestClient("http://localhost:64648"); //replace with your domain name
var request = new RestRequest("/Home/DoJob", Method.POST); //replace 'Home' with your controller name
request.RequestFormat = DataFormat.Json;
request.AddBody(new { id = message });
client.Execute(request);

还有我的端点定义

[HttpPost]
public HttpResponseMessage DoJob([System.Web.Http.FromBody] string id) {
  //some code
}

一切正常。

顺便说一句,如果你想要 post 数组你只需要改变两个地方:

request.AddBody(new { ids = message.Split('.') });

和定义

[HttpPost]
public HttpResponseMessage DoJob([System.Web.Http.FromBody] string[] ids) {
  //some code
}

我用另一种方式解决了传递正文的问题。

而不是:

request.AddParameter("text/json", message, ParameterType.RequestBody);

我输入:

request.RequestFormat = DataFormat.Json;
request.AddBody(message);

现在无论消息中包含什么字符,消息内容(只要是 json)都会正确传递到我的端点