1
votes

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.

2
The error suggests you may have forgotten to "Set the type to JSON (application/json).". - serpent5
@KirkLarkin thank you, I've managed to fix it now - Tieske

2 Answers

4
votes

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

0
votes

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

thank you for your suggestion!