Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
Unsupported media type ASP.NET Core Web API

On the front-end i use Angular to collect som data from the form and send it to my server-side controllers. As the image shows below, i get the data ($scope.newData) on my controller and service, but when it reaches the server, i get the following error: "Unsupported media type" and my newData is empty.

Website console

My controller:

// Create new salesman
  $scope.addSalesman = function (newData) {
    console.log("Controller");
    console.log($scope.newData);
    myService.addNewSalesman($scope.newData).then(function (data) {
      console.log(data);
    }, function (err) {
      console.log(err);
    });
  };

My service:

addNewSalesman: function (newData) {
            console.log("service");
            console.log(newData)
            var deferred = $q.defer();
            $http({
                method: 'POST',
                url: '/api/Salesman',
                headers: { 'Content-type': 'application/json' }
            }, newData).then(function (res) {
                deferred.resolve(res.data);
            }, function (res) {
                deferred.reject(res);
            });
            return deferred.promise;
        }

My Salesman model:

public class Salesman
    {
        public int SalesmanID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string BirthDate { get; set; }
        public int Phone { get; set; }
        public string Adress { get; set; }
        public string City { get; set; }
        public int Postal { get; set; }
        public string Role { get; set; }
    }

My server-side controller:

[Route("api/[controller]")]
public class SalesmanController : Controller
{

    private readonly DataAccess _DataAccess;

    public SalesmanController() 
    { 
        _DataAccess = new DataAccess(); 
    } 

    [HttpPost]
    public IActionResult PostSalesman([FromBody] Salesman newData)
    {
        return Ok(newData); 
    }
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Just replace [FromBody] to [FromForm] in your controller.

about 4 years ago · Santiago Trujillo Report

0

The header you are sending is wrong. You are sending Content-Type: application/json, but you have to send Accept: application/json.

Content-Type: application/json is what the server must send to the client and the client must send Accept to tell the server which type of response it accepts.

addNewSalesman: function (newData) {
        console.log("service");
        console.log(newData)
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: '/api/Salesman',
            headers: { 'Accept': 'application/json' }
        }, newData).then(function (res) {
            deferred.resolve(res.data);
        }, function (res) {
            deferred.reject(res);
        });
        return deferred.promise;
    }

Should do it. Also see "Content negotiation" on MDN.

about 4 years ago · Santiago Trujillo Report

0

This is a CORS issue.

During development it's safe to accept all http request methods from all origins. Add the following to your startup.cs:

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        //Accept All HTTP Request Methods from all origins
        app.UseCors(builder =>
            builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());

        app.UseMvc();
    }

See here for more details about CORS.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!