Return Web API Post 415 - Unsupported Media Type

How to perform CRUD operations in a SQL Server database using C #?

For example, let's say that there is a DB table Employeeswith these columns:

EmployeeID, FirstName, PostalCode

and I want to publish a new employee with information in this database. What would be an effective method for this?

My current HttpPost:

namespace API.Controllers
{
    public class EmployeesController : ApiController
    {
        [HttpPost]
        public void AddEmployee (Employee employee)
        {
            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = @"Server=.\servername;Database=Northwind;User ID=Username;Password=password;";

            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.CommandText = "INSERT INTO Employee (EmployeeID,FirstName,PostalCode) Values (@EmployeeID, @FirstName, @Zip)";

            sqlCmd.Parameters.AddWithValue("@EmployeeID", employee.EmployeeID);
            sqlCmd.Parameters.AddWithValue("@FirstName", employee.FirstName);
            sqlCmd.Parameters.AddWithValue("@Zip", employee.Zip);

            myConnection.Open();
            int rowInserted = sqlCmd.ExecuteNonQuery();
            myConnection.Close();
        }
    }
}

And here is the model Employee:

namespace API.Models
{
    public class Employee
    {
        public int EmployeeID { get; set; }
        public string FirstName { get; set; }
        public int Zip { get; set; }
    }
}

When I test my API using Fiddler with the following input:

{ "EmployeeID":91, "FirstName":"Vader", "Zip":94221}

I get this error:

HTTP / 1.1 415 Unsupported Media Type

and nothing is inserted into the database.

Everyone is welcome any guide.

WebApiConfig.cs:

namespace API
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

Unprocessed error message generated by request:

HTTP / 1.1 415 Unsupported Media Type

Cache-control: no-cache

Pragma: no-cache

Content-Type: application / json; encoding = UTF-8

Expires: -1

Server: Microsoft-IIS / 10.0

X-AspNet-Version: 4.0.30319

X-SourceFiles: =? UTF-8? B? YzpcdXNlcnNcYW1lc2tvXGRvY3VtZW50c1x2aXN1YWwgc3R1ZGlvIDIwMTVcUHJvamVjdHNcQVBJXEFQSVxhcGlcRW1wbG95ZWVzXEFkZEVtcGxveWVl? =

X-Powered-By: ASP.NET

: , 22 2016 15:33:06 GMT

Content-Length: 986

{ "": " , Content-Type. " /- " .",

"ExceptionMessage": "No MediaTypeFormatter " " " application/octet-stream " .",

"ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",

"StackTrace": " System.Net.Http.HttpContentExtensions.ReadAsAsync [T] ( HttpContent, , IEnumerable1, IFormatterLogger, ). ReadAsAsync ( HttpContent, , IEnumerable1, IFormatterLogger formLogLog, CancellationToken cancelationToken)\r\n System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync( HttpRequestMessage, , IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancelationToken )" }

+4
1

JSON? application/json

"API/{}/{}/{ID}"

API URL

HTTP://: 63433/API/ /AddEmployee

URL

{"EmployeeID": 91, "FirstName": "Vader", "Zip": 94221}

Post application/json

API ,

, .

0

Source: https://habr.com/ru/post/1648810/


All Articles