-1

I got this error

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:{ Transfer-Encoding: chunked Server: Microsoft-IIS/10.0 X-Powered-By: ASP.NET Date: Tue, 02 May 2023 10:38:30 GMT Content-Type: application/problem+json; charset=utf-8}}

After I updated the .net from 3.1 to 7.0.

Here is the rest client code:

var obj = new Login();

using var httpClient = new HttpClient();

HttpClientHelper.SetupClient(httpClient);
StringContent content = new StringContent(JsonConvert.SerializeObject(loginObj), Encoding.UTF8, "application/json");
using var response = await httpClient.PostAsync(apiName + "/UserLogin", content);

string apiResponse = await response.Content.ReadAsStringAsync();
obj = JsonConvert.DeserializeObject<Login>(apiResponse);
httpClient.Dispose();
response.Dispose();

return obj;

The same code was running fine.

UPDATE I discovered that objects containing some null values are not accepted after updating .NET version.

mado HK
  • 3
  • 2
  • Do your server side logs tell you what the problem is with the request? Just looking at the client code might not be enough to get behind the problem or hoping that someone guesses the problem. Since you get a "problem+json" back you might want to look at the body of this request. It might contain helpful details. – Ralf May 02 '23 at 10:55
  • How to see the logs – mado HK May 02 '23 at 11:06
  • Can't say. But if you don't know then you presumably didn't implemented anything and you are running your app blind. It may be an option to debug your server or if you really use a standard server (it looks like you are using IIS) then it maybe already logging something per request you can look at. – Ralf May 02 '23 at 11:18
  • I'm not entirely sure, but I think the 'content' being sent may be problematic. Could you try using 'JsonSerializer.Serialize' instead of 'JsonConvert.SerializeObject'? – emrah karaman May 02 '23 at 11:40
  • "The request does not reach the breakpoint in API project; it goes directly to the next line. However, the request from Postman reaches the breakpoint and works fine. – mado HK May 02 '23 at 12:01
  • Just based on this code can't get any useful information, can you check the request body to check the difference between it send by postman? – Xinran Shen May 03 '23 at 05:31
  • @XinranShen After updating to .NET 7, I discovered that objects containing null values are not accepted. – mado HK May 03 '23 at 08:14
  • @mado HK, YES, from .net 6, the property become default Non-nullable. – Xinran Shen May 03 '23 at 08:23
  • @XinranShen how to change that? – mado HK May 03 '23 at 08:27
  • You can refer to this [link](https://stackoverflow.com/questions/74956285/razor-pages-modelstate-is-different-invalid-when-nullable-types-are-enabled/74958788#74958788) – Xinran Shen May 03 '23 at 08:47

1 Answers1

-2

when I see 400 error after updating to net 5+, the first thing I recommend is to remove a nullable option from a project file

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <!--<Nullable>enable</Nullable>-->
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

or you have to add " ? " to all your properties that have null as default value of all classes that are controller action input parameters. Otherwise API controller returns validation error 400

Serge
  • 40,935
  • 4
  • 18
  • 45