ASP.NET Core Web API unsupported Media type

Hi I’m trying to build my first API wit ASP.net Core and have been following this tutorial: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio

I’d like to find out why I’m unable to post anything to my database (which I have setup in previous steps)

I’m using songs instead of todo’s and when I try to make a POST request in postman I get this response

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
    "title": "Unsupported Media Type",
    "status": 415,
    "traceId": "|25aa76f-4966fce03006b505."
}

It doesn’t matter what key:value pairs I send to my API, when using the post method I will always get the same response.

my Song class looks like this:

    public class Song
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public string Artist { get; set; }

        [DataType(DataType.Date)]
        public DateTime ReleaseDate { get; set; }

        public string Spotify { get; set; }

        public string Youtube { get; set; }

        public string Instagram { get; set; }
    }

This is the link to the exact step I’m at:
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio#test-posttodoitem-with-postman

ps. I’m still very new when it comes to programming and it will probaly been something super obvious.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In the body request, you are sending an invalid JSON. you don’t set content type right in the header.

To solve it in the POSTMAN, click in Text and select the JSON option from the drop-down list. Then add the open and closed braces. So that you receive an HTTP 400 code. Send your object like the following snippet.

{
    "FirstName":"Tom",
    "LastName":"Anderson",
    "Address":"Boston"
}

For detail information, click here

Method 2

Yes that was it, inside postman I had to change a header from text/plain to application/json!

thank you for your suggestion!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x