0
votes

I'm trying to create a Self-Hosted service to be the central point for my Logs in my applications. So i create my self-hosted service in OWIN and installed my service. The service is working fine and i can get it to work with a GET method through an AJAX call via JQuery. My problem come from the POST method, because i want to post many field to the WEB API (preferably an object that containt everything) but it doesn't work at all... and i get this error message

415 -> "Unsupported Media Type"

"The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'Log' from content with media type 'application/octet-stream'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable'1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"

I can send a POST without parameter and it work... but i need those parameters!

Here is my code:

LOG CLASS

public class Log
{
    public string Message { get; set; }
    public string ApplicationName { get; set; }
}

WEB API CONTROLLER

public class MessagingController : ApiController
{
    [HttpPost]
    public void Log([FromBody] Log log)
    {
        var path = Program.ConfigurationService.GetConfigValue(ConfigurationService.RootPathConfig);

        using (var writter = new StreamWriter(path + "TEST.txt"))
        {
            writter.Write(log.ApplicationName + " -> " + log.Message);
        }
    }
}

AJAX call to the WEB API

    var obj = {
        applicationName: "APP",
        message: "MESS"
    }

    $.ajax({
        type: "POST",
        cache: false,
        url: "http://localhost:9001/api/messaging/log",
        data: JSON.stringify(obj),
        dataType: 'json',
        contentType: "application/json; charset=utf-8; Access-Control-Allow-Origin;",
        async: true,
        success: function(resp) {
            console.log(resp);
         },
         error: function(err) {
            console.log(err);
         }
    });
1

1 Answers

1
votes

Ok, i found my answer in this tutorial!

http://www.codeproject.com/Articles/742532/Using-Web-API-Individual-User-Account-plus-CORS-En

i removed somes data of the AJAX request and now its doing its job perfectly!!

here is my new AJAX :

var obj = {
    applicationName: "APP",
    message: "MESS"
}

$.ajax({
    type: "POST",
    url: "http://localhost:9001/api/messaging/log",
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    data: obj,
    success: function(resp) {
        console.log(resp);
     },
     error: function(err) {
        console.log(err);
     }
});

Ok with Fiddler i found that he real ContentType that i was needing is :

contentType: "application/x-www-form-urlencoded; charset=UTF-8",