1

Closed

The format of value 'application/json; charset=utf-8' is invalid.

description

I found this error happening when trying to do a Unit testing or sometimes integeration with Web API

public void Should_Get_The_Collection_State_url_for_reloading_the_List()
    {
        var configuration = new HttpConfiguration();
        RouteConfig.RegisterRoutes(configuration.Routes);
        var server = new HttpServer(configuration);
        var controller = new MazeController();
        var client = new HttpClient(server);
        var result = client.GetAsync("http://localhost:8081/api/Maze").Result;
    }
above code is the simple one which i am trying to test the output.Please let me know how to solve the above issue
Closed Jun 5, 2013 at 8:17 PM by kichalla
Unable to repro this issue. Also, there isn't much information in the bug to repro.

comments

roncain wrote Jul 18, 2012 at 12:33 PM

Do you have a stack trace you can attach, or more details of what MazeController does?
This exception comes from System.Net.Http when it is parsing a media type header value, so it would be informative to discover where this is being done.

The likely cause is a 'new MediaTypeHeaderValue("application/json; charset=utf-8")'. You can verify this failure with a unit test that invokes the ctor like that.

Normal convention is to construct the MediaTypeHeaderValue with just the base media type ("application/json") and then set the Charset property.

Alternatively, use MediaTypeHeaderValue.Parse("application/json; charset=utf-8"),

Could you please verify whether this error is coming from your code or from the WebApi code?

HenrikN wrote Jul 21, 2012 at 5:37 PM

Please use "utf8" instead of "utf-8". That is, remove the "-". Then it should work.

Henrik

satish860 wrote Jul 21, 2012 at 8:45 PM

Hi Henrik . Thanks for the reply. But as you can see from the code . i didnt specify any encoding style. According to stacktrace it occurs when trying to CreateResponse<T> Extension method.