Home > Software engineering >  Postman status 400 Bad Request when sending data to ASP.NET Core API
Postman status 400 Bad Request when sending data to ASP.NET Core API

Time:05-07

I currently have a problem where Postman gives an 400 Bad Request response that tells some fields are required. This is my json object that I send with Postman:

{
"Id": "2", 
"Content": "Een bericht", 
"Likes": 0, 
"PublishDate": "2022-05-05", 
"User_Id": "1"
}

And here is the response from postman:

{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-c29c164c5467d68f12bdc49bec8b1b09-f14521a6d718ce63-00",
"errors": {
    "Id": [
        "The Id field is required."
    ],
    "Content": [
        "The Content field is required."
    ],
    "User_Id": [
        "The User_Id field is required."
    ]
}
}

I made the API with ASP.NET Core where I send the request to. In the API, I have my model:

public class Tweet
{
    public string Id { get; private set; }
    public string Content { get; private set; }
    public int Likes { get; private set; }
    public DateTime PublishDate { get; private set; }
    public string User_Id { get; private set; }

    public Tweet()
    {
    }

    public Tweet(string id, string content, int likes, DateTime publishDate, string user_Id)
    {
        Id = id;
        Content = content;
        Likes = likes;
        PublishDate = publishDate;
        User_Id = user_Id;
    }

    
}

And here is my controller with the post method:

[Route("api/[controller]")]
[ApiController]
public class TweetController : ControllerBase
{
    private ITweetRepo _repository;

    public TweetController(ITweetRepo repo)
    {
        _repository = repo;
    }


    // POST api/<TweetController>
    [HttpPost]
    public ActionResult<Tweet> PostTweet([FromBody] Tweet tweet)
    {
        try
        {
            if (tweet == null)
            {
                return BadRequest("Tweets' message is null or message invalid");
            }

            var createdTweet = _repository.CreateTweet(tweet);

            return CreatedAtAction(nameof(PostTweet), createdTweet);
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

After some investigation, I noticed that the fields that Postman's response is talking about are Non-nullable properties. enter image description here Maybe these warning in visual studio have something to do with my problem?

I really appreciate any suggestions. Thank you in advance

CodePudding user response:

Remove both constructors from the Tweet class. The values are overridden when the Tweet object is instantiated.

The properties have to be set as nullable

public int? Likes {get;set;}

The constructor can accept null values in the post

Web.Api

CodePudding user response:

In .NET 6 the non-nullable property must be required, otherwise the model validation will fail and you will get 400 Bad Request error.

In your scenario, you use private setter which makes property readonly. That is to say no matter you set the value for property or not, the model always cannot be bound successfully, which cause the model validation failure.

If you actually want to send the data to backend, you need remove the private access modifier:

public class Tweet
{
    public string Id { get;  set; }
    public string Content { get;  set; }
    public int Likes { get;  set; }
    public DateTime PublishDate { get;  set; }
    public string User_Id { get;  set; }

    public Tweet()
    {
    }
    public Tweet(string id, string content, int likes, DateTime publishDate, string user_Id)
    {
        Id = id;
        Content = content;
        Likes = likes;
        PublishDate = publishDate;
        User_Id = user_Id;
    }
}
  • Related