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.
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);
}
Fixing 415 Unsupported Media Type errorsEnsure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.
The HTTP 415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. The format problem might be due to the request's indicated Content-Type or Content-Encoding , or as a result of inspecting the data directly.
[FromBody] attributeThe ASP.NET Core runtime delegates the responsibility of reading the body to an input formatter. Input formatters are explained later in this article. When [FromBody] is applied to a complex type parameter, any binding source attributes applied to its properties are ignored.
The FromForm attribute is for incoming data from a submitted form sent by the content type application/x-www-url-formencoded while the FromBody will parse the model the default way, which in most cases are sent by the content type application/json , from the request body.
At a guess under the hood, a controller method that expects a (serialised) parameter would expect the request content type (MIME) to be application/json - but the HttpClient calling POST/PUT without a parameter does not pass this (maybe just text/html) - and hence we get 415 'Unsupported Media Type'.
This error mainly occurs when you try to send a body and you haven't specified the media-type through the Content-Type header. Ensure that the request is GET and your body is empty.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With