Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular post request with content-type application/json not working

I try to send post request with angular but when i set content-type to application/json the request content-type always not set and the HTTP method always removed and all posted data removed from request body here my code

enter image description here

and here my request

enter image description here

like image 608
Moneim Avatar asked Dec 21 '25 18:12

Moneim


1 Answers

the HTTP method always removed and all posted data removed from request body

In the screenshot you shared, we can find that the request you captured is a OPTIONS request (preflight request), not actual POST request. So the data you posted is not in request body.

Besides, the following code snippet works well on my side, you can refer to it.

var student = {'name' : 'testuser', 'age' : 29};

const headers = new HttpHeaders().set('Content-Type','application/json');

this.http.post<Student>('https://xxxx/student',JSON.stringify(student),{headers:headers})
.subscribe(data => {
  console.log(data);
});

Controller and Action

[Route("[controller]")]
[ApiController]
public class StudentController : ControllerBase
{
    [HttpPost]
    public IActionResult Post(Student student)
    {
        return Ok(student);
    }
}

Test Result

enter image description here

like image 195
Fei Han Avatar answered Dec 24 '25 07:12

Fei Han