title: 415 (Unsupported Media Type) angular 4 Post
body: <p>I am trying to access a wep api with angular 4 post method.</p>
  
  <p>In my service, I've added content-type of application/json. And I'm converting the object into json while sending data to api. I am using HttpClientModule</p>
  
  <pre><code>import { Injectable } from '@angular/core';
  import { HttpClient } from '@angular/common/http';
  
  @Injectable()
  
  export class NewServiceService {
  
    baseUrl = "http://localhost:33969/api/";
    headers = { headers: new Headers({ 'Content-Type': 'application/json' }) 
        };
    obj= {
      name:"A",
      cgpa: 3
    };
  
  _http:any;
  constructor(http: HttpClient) {
      this._http = http;
  }
  
  SaveStudents(){
  
      this._http
      .post(
          this.baseUrl + 'home/Save', 
          JSON.stringify(this.obj),
          this.headers
       )
    .subscribe(
      res =&gt; {
        alert("Student Saved!");
      },
      err =&gt; {
        alert("Error!");
      }
    );
  }}
  </code></pre>
  
  <p>In the API,</p>
  
  <pre><code>using Entity;
  using Microsoft.AspNetCore.Mvc;
  using Repo;
  
  namespace API_Core.Controllers
  {
  [Produces("application/json")]
  [Route("api/[controller]/[action]")]
  
  public class HomeController : Controller
  {
      IStudent _student;
      public HomeController(IStudent student)
      {
          _student = student;
      }
  
      [HttpPost]   
      public Student Save([FromBody]Student s)
      {
          return _student.Save(s);
      }
  }
  }
  </code></pre>
  
  <p>here, I  want to catch the objtect as Student model and do something with the data. Here is the Student Model</p>
  
  <pre><code>public class Student
  {
      [Key]
      public int ID { get; set; }
  
      public string Name { get; set; }
  
      public double Cgpa { get; set; }
  }
  </code></pre>
  
  <p>But when using prostman, I could succesfully receive the object.<a href="https://i.stack.imgur.com/yGpQQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/yGpQQ.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE</strong>
  using HttpHeaders instead of Headers and CORS solved the issue </p>
  
  <p>Enabling CORS for ASP.NET Core 2 =></p>
  
  <p>In ConfigureServices:</p>
  
  <pre><code>services.AddCors(options =&gt; options.AddPolicy("Cors", builder =&gt;
          {
              builder
              .AllowAnyOrigin()
              .AllowAnyMethod()
              .AllowAnyHeader();
          }));
  </code></pre>
  
  <p>In Configure(Above usemvc()):</p>
  
  <pre><code>app.UseCors("Cors");
  </code></pre>