I have created a user register controller to register users with repository design pattern. My controller looks like this.
[Route("api/[controller]")]
public class AuthController : Controller
{
private readonly IAuthRepository _repo;
public AuthController(IAuthRepository repo)
{
_repo = repo;
}
[AllowAnonymous]
[HttpPost("register")]
public async Task<IActionResult> Register([FromBody] UserForRegisterDto userForRegisterDto){
// validate request
if(!ModelState.IsValid)
return BadRequest(ModelState);
userForRegisterDto.Username = userForRegisterDto.Username.ToLower();
if(await _repo.UserExists(userForRegisterDto.Username))
return BadRequest("Username is already taken");
var userToCreate = new User{
Username = userForRegisterDto.Username
};
var createUser = await _repo.Register(userToCreate, userForRegisterDto.Password);
return StatusCode(201);
}
}
When I send a request using Postman, it gives me the the 404 not found status code, and API reports the request completed without reading the entire body.
My request in Postman looks like this.
I have used Data Transfer Objects(DTO) to encapsulate data, I removed UserForRegisterDto
and tried to use string username
and string password
, as follows but it did not work.
public async Task<IActionResult> Register([FromBody] string username, string password)
UserForRegisterDto
looks like this.
public class UserForRegisterDto
{
[Required]
public string Username { get; set; }
[Required]
[StringLength(8, MinimumLength =4, ErrorMessage = "You must specify a password between 4 and 8 characters.")]
public string Password { get; set; }
}
I have tried many online solutions for this, but so far nothing resolved my problem. Please help me to troubleshoot the issue, Thank you in advance. I'm running this API on Ubuntu 18.04
Edit: Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataContext>(x => x.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCors();
services.AddScoped<IAuthRepository, AuthRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
app.UseMvc();
}
}
It happened to me in a new ASP.NET Core 2.1 service when debugging in localhost because I had in Startup.Configure:
app.UseHttpsRedirection();
I deactivated this setting when debugging locally:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHttpsRedirection();
}
The error info of the application completed without reading the entire request body
often occurs when the client send a request that doesn't fulfill the sever requirements . In other words , it happens just before entering the action , resulting that you cannot debug it via a breakpoint within the body of action method .
For example , let's say a action method on the server :
[Route("api/[controller]")]
[ApiController]
public class DummyController : ControllerBase
{
[HttpPost]
public DummyDto PostTest([FromBody] DummyDto dto)
{
return dto;
}
}
The DummyDto
here is a dummy class to hold information:
public class DummyDto
{
public int Id { get; set; }
}
When clients send a request with payload not well formatted
For example , the following post request , which doesn't have a Content-Type: application/json
header :
POST https://localhost:44306/api/test HTTP/1.1
Accept : application/json
{ "id":5 }
will result in a similar error info :
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 POST http://localhost:44306/api/test 10
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 1.9319ms 404
Microsoft.AspNetCore.Server.Kestrel:Information: Connection id "0HLGH8R93RPUO", Request id "0HLGH8R93RPUO:00000002": the application completed without reading the entire request body.
and the response from the server will be 404
:
HTTP/1.1 404 Not Found
Server: Kestrel
X-SourceFiles: =?UTF-8?B?RDpccmVwb3J0XDIwMThcOVw5LTFcU08uQXV0aFJlYWRpbmdXaXRob3V0RW50aXRlQm9keVxBcHBcQXBwXGFwaVx0ZXN0?=
X-Powered-By: ASP.NET
Date: Mon, 03 Sep 2018 02:42:53 GMT
Content-Length: 0
As for the question you described , I suggest you should check the following list :
Content-Type: application/json
? make sure you have checked the header code
to show what it sends exactly when you send a request to the server .There can be multiple reasons out of which one can be : – Caching in Visual Studio --
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With