.NET Core 1.0 Web Api processing request body as JSON when content-type is text/plain

后端 未结 1 1484
走了就别回头了
走了就别回头了 2021-01-14 20:49

A vendor API I need to use is sending a POST request with content-type: text/plain and JSON in the body.

How do I parse it in .net core 1.0 web api?

I\'m s

相关标签:
1条回答
  • 2021-01-14 20:58

    I made it work by adding the text/plain content-type to the JsonInputFormatter in Startup.cs ConfigureServices() method, like so:

            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc(config =>
                {
                    foreach (var formatter in config.InputFormatters)
                    {
                        if (formatter.GetType() == typeof(JsonInputFormatter))
                            ((JsonInputFormatter)formatter).SupportedMediaTypes.Add(
                                MediaTypeHeaderValue.Parse("text/plain"));
                    }
                });
                ...
             }
    

    Edit: I made a seed project for the SNS client lambda, that parses the message and confirms the subscription out the box.

    0 讨论(0)
提交回复
热议问题