Home > Software engineering >  Returns 400 Bad Request by POST, PUT methods but GET is working okay
Returns 400 Bad Request by POST, PUT methods but GET is working okay

Time:05-07

I am trying to call POST/PUT(by using HttpClient, in Swagger or Postman it works) method but it is return 400 status code(Bad Request). It checks for database exist and it is returns okay.

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
  Executed DbCommand (27ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
  SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
  Executed DbCommand (42ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

  IF EXISTS
      (SELECT *
       FROM [sys].[objects] o
       WHERE [o].[type] = 'U'
       AND [o].[is_ms_shipped] = 0
       AND NOT EXISTS (SELECT *
           FROM [sys].[extended_properties] AS [ep]
           WHERE [ep].[major_id] = [o].[object_id]
               AND [ep].[minor_id] = 0
               AND [ep].[class] = 1
               AND [ep].[name] = N'microsoft_database_tools_support'
      )
  )
  SELECT 1 ELSE SELECT 0

Also I am using this part of code :

var user = new User()
        {
            UserName = "dasdas",
            PhoneNumber = "dfsdfsdf",
            FirstName = "fdsfsd",
            LastName = "fdsfsdfsdfsd"
        };
        var json = JsonConvert.SerializeObject(user);
        var data = new StringContent(json, Encoding.UTF8, "application/json");
        using var client = new HttpClient();
        string url = Client.BaseAddress   RequestType.User;
        var response = client.PostAsync(url, data).Result;

I tried to use another API and it works but in my API there is no errors and so on.

CodePudding user response:

As this document said:

Beginning with .NET 6, new projects include the <Nullable>enable</Nullable> element in the project file. Once the feature is turned on, existing reference variable declarations become non-nullable reference types.

In .NET 6 the non-nullable property must be required, otherwise the model validation will fail.

To achieve your requirement, you can remove <Nullable>enable</Nullable> from your project file.

  • Related