WebApi不接受RestSharp AddJsonBody

时间:2018-08-10 19:04:44

标签: asp.net-web-api restsharp

我有一个WebApi端点,可以通过curl ok来调用:

curl -X POST "https://endpoint/SendPin?email=john@smith.com" -H  "accept: application/json"

WebAPI看起来像这样:

    [HttpPost]
    [Route("SendPin")]
    public async Task<bool> SendPin([Required] [EmailAddress] string email)
    {
        ....
    }

问题::当我使用RestSharp调用服务器时,服务器返回BadRequest消息“需要电子邮件”?

    var request = new RestRequest("SendCode");
    request.AddJsonBody(new { Email = "john@smith.com"});
    RestClient client = new RestClient(TheUrl);
    var response = await client .ExecutePostTaskAsync<bool>(request );

RestSharp似乎没有将其作为参数发送到网址?

2 个答案:

答案 0 :(得分:0)

您正在使用request.AddJsonBody()方法将电子邮件添加到请求正文中。 相反,像在curl请求中一样,将其作为查询字符串添加到请求url

var request = new RestRequest("SendPin"+ "?email=john@smith.com");
RestClient client = new RestClient(TheUrl);
var response = await client.ExecutePostTaskAsync<bool>(request);

答案 1 :(得分:0)

由于参数是一种简单的类型,因此我需要像他一样调用它:

<div class="input-group">
  <input type="text" ngbDatepicker placeholder="{{model}}" class="form-control" #d="ngbDatepicker" [(ngModel)]="model" />
  <button (click)="d.toggle()">Toggle</button>

</div>