0

I have an angular app with a .net core web-api as the back end. the get request from the client works as expected and data from the server is fetched.

but when i try to post from the client to the server i get a 405 method not allowed error.

i have implemented the CORS middle ware found at this answer :

    httpContext.Response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:4200");
    httpContext.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
    httpContext.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name");
    httpContext.Response.Headers.Add("Access-Control-Allow-Methods", "POST,GET,PUT,PATCH,DELETE,OPTIONS");

web-api controller

[Route("api/[controller]")]
[ApiController]
public class PostsController : ControllerBase
{
    [HttpGet]
    public ActionResult<List<Post>> Get()
    {
        return _repository.Get().ToList();
    }

    [HttpPost]
    public IActionResult Post([FromBody] Post post)
    {
        if (post == null)
        {
            return BadRequest();
        }
        _repository.Add(post);
        _repository.Save();
        return CreatedAtRoute("GetPostById", new { ID = post.Id }, post);
    }
}

Post model on server

   public class Post 
    {
        [Key]
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
    }

app.component.ts

  apiUrl = 'http://localhost:33633/api/posts';
  constructor(private http: HttpClient) {}

onCreatePost(postData: { title: string; content: string }) {

this.http.post(this.apiUrl, postData).subscribe(responseData =>
{
  console.log(responseData);
})
}
  onFetchPosts() {
    this.http.get(this.apiUrl).subscribe(responseData =>
    {
      console.log(responseData);
    })
  }

console error when trying to post to server: enter image description here

what could be the reason for this error?

---Edit - found a solution--- in this answer

i added a base controller with the method

public HttpResponseMessage Options()
{
  return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}

and now it works.

kifka
  • 96
  • 1
  • 9

1 Answers1

0

It means that the HTTP verb OPTIONS is not supported by the URL you are calling.

Such requests are fired automatically even though you only fire POST or GET from your code. Read https://stackoverflow.com/a/40497696/5389127.

You have to allow OPTIONS requests on your backend.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
  • correct me if I am wrong here, but for my understanding if the options request fails in angular - the browser will not send the post request to the server. the CORS on the server allows all these methods: POST, GET, PUT, PATCH, DELETE, OPTIONS. how can i allow the OPTIONS http verb on the web api controller? – kifka Jul 13 '21 at 04:27
  • You're right, if the `OPTIONS` request does not succeed, no other requests will be made. I'll edit my answer. How to allow OPTIONS on your backend is entirely dependent on your backend tech/framework. I'd suggest posting another question dedicated to your backend. – Gaël J Jul 13 '21 at 06:59
  • thank you for the related informative answer, I have edited my post with the solution that I found in another stack overflow answer. – kifka Jul 13 '21 at 17:18