Search code examples
asp.netangularswashbuckle.aspnetcore

Angular 16 sending null values to API


I'm trying to send an object to my api but i always get null values.

Here is my angular method:

createNormalPost(post: CreateNormalPostInterface) {
  const formData = 'ceasdasdas'

  return this.httpClient.post<any>(this.API, formData,
    {headers: {'Content-Type': 'multipart/form-data'}}
  );
}

And here is my controller:

[Route("api/[controller]")]
 [ApiController]
 [AllowAnonymous]
 public class PostController : ControllerBase
 {
     private readonly IPostService _postService;
     private readonly ILogger<AuthController> _logger;

     public PostController(IPostService postService, ILogger<AuthController> logger)
     {
         _postService = postService;
         _logger = logger;
     }
     [HttpPost]
     public async Task<IActionResult> CreatePostNormalAsync([FromForm] string formData)
     {
         try
         {

             Console.WriteLine(formData);
             //var response = await _postService.CreatePostNormalAsync(post);
             return Ok(formData);
         }
         catch (Exception ex)
         {
             _logger.LogError(ex, ex.Message);
             return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
         }

     }
 }

I'm currently trying to send a string just to check if the data is being sent but i still get null.

EDIT: I think i found the problem, somehow the request is sent as 'application/json' not as form data.


Solution

  • It was the interceptor that keeps overriding my request headers.