我有一个 RestApi,我想将它与我的 c# mvc 客户端一起使用。
我需要发送一些由 ac# 模型表示的数据包。
我尝试使用 RestSharp 代码:
//model = UserModel
var client = new RestClient(apiUrl);
var request = new RestRequest("/user/register", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddParameter("user", model);
request.AddHeader("Content-Type", "application/json; charset=UTF-8");
var response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
在response.StatusCode中我有BadRequest System.Net.HttpStatusCode
我如何调试我的请求?我尝试使用wireshark 查看请求,但没有看到任何json 数据包。
编辑:
当我使用 AddJsonBody 而不是 AddParameter 时,它可以工作,但我的 json 数据包的格式不符合预期。
我想
{
"user":
{"Username": "Test",
"Password": "password"
}
}
Run Code Online (Sandbox Code Playgroud)
使用 AddJsonBody 我有:
{
{
"Username": "Test",
"Password": "password"
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用 RestSharp 在 json 数据包中给出“名称”?
小智 5
你可以使用这个:
request.AddJsonBody(new { user = model });
Run Code Online (Sandbox Code Playgroud)
这将创建一个匿名类型,其属性为“user”,其中包含您的模型。然后 json 序列化器应该生成预期的字符串。
| 归档时间: |
|
| 查看次数: |
4827 次 |
| 最近记录: |