当我从邮递员向本地API发送请求时,无论发送哪个参数都会产生正确的结果。但是,当我向服务器发送相同的请求时,出现400错误(错误请求)。
我能够将标头中的参数发送到服务器,但是无法从正文中发送参数。
这是我的请求
POST http://takestan.sytes.net/api/User/UserRegister HTTP/1.1
Host: takestan.sytes.net
Connection: keep-alive
Content-Length: 52
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
Postman-Token: 09b7c458-5781-6537-5992-f5bcb26022a7
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip, deflate
{"username": "Jignesh", "password": "password"}
这是我的服务器代码Asp.net core2.2
[Route("api/[controller]")]
public class UserController : ControllerBase
{
IRacerDataProvider _racerDataProvider;
public UserController(IRacerDataProvider racerDataProvider, IOptions<AppSettings> appSettings)
{
_racerDataProvider = racerDataProvider;
}
[HttpPost("UserRegister")]
public object UserRegister([FromBody]Racer racer)
{
return new { name = racer.Username, Password = racer.Name };
}
}
这是我的Racer是模特
public class Racer
{
[Required]
public string UserName{ get; set; }
public string Name { get; set; }
}