skip to Main Content

I have two projects running in Visual Studio, one on .NET 6 and one on .NET 8. The .NET 6 one is sending a POST request to the .NET 8 one, but the single parameter of the .NET 8 method always gets a null.

I created two examples below. I took out a lot of the complicated code and just made a simple example trying to pass a string over the request.

.NET 6 project sending the POST request:

Uri RecommendationServiceUrl = new Uri(http://localhost:58815);
string message = "Hi";

using (var client = new HttpClient { BaseAddress = RecommendationServiceUrl })
{
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var content = new StringContent(JsonConvert.SerializeObject(message));

    var response = await client.PostAsync($"api/recommendations/getrecommendation", content);
}

.NET 8 project receiving the POST request:

[HttpPost]
[Route("api/recommendations/getrecommendation")]
public IActionResult Post(string test)
{
    return Ok();
}

If I put a breakpoint in this method on the return and check the test parameter’s value, it’s always null no matter what I try. I’ve done tons of Googling and found other people asking the same question but the solution never works for me.

I’ve tried:

  • Changing the method to public async Task<IActionResult>
  • Adding [FromBody], [FromQuery], or [FromForm] to the parameter, all of them either do nothing and the value remains null, or the request fails for a 415 error and never reaches the receiving service.

I do know that the previous incarnation of the receiving .NET 8 service used to work fine and have a parameter when getting the POST request when it was a .NET 4.7.2 project. But since I’ve upgraded it to .NET 8 it no longer works. Maybe there’s some kind of boilerplate code needed during the app’s startup that is needed with .NET 8, but aside from services.AddControllers();, services.AddHttpClient();, and

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

in my startup.cs file, I don’t know what else would be needed.

I’m not very good with this HTTP stuff. So any suggestions would be greatly appreciated. Thank you.

2

Answers


  1. string message = "Hi";
    Console.WriteLine(JsonConvert.SerializeObject(message));
    // Hi
    

    So, server expected a string of json but got a plain text, you named the field test but nothing indicates the "Hi" is for the test filed.
    You can use a FormUrlEncodedContent, or simply serialize a dynamic object

    Uri RecommendationServiceUrl = new Uri(http://localhost:58815);
    dynamic message = new
    {
        Test = "Hi"
    };
    using (var client = new HttpClient { BaseAddress = RecommendationServiceUrl })
    {
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        var content = new StringContent(JsonConvert.SerializeObject(message));
    
        var response = await client.PostAsync($"api/recommendations/getrecommendation", content);
    }
    
    

    You may need to add [FromBody] or [FromForm] to the parameter back according to your choice.

    Login or Signup to reply.
  2. Your API accepting value from Query string & You are passing through JSON, That’s why you are receiving this error.

    Try this code:

    Uri RecommendationServiceUrl = new Uri(http://localhost:58815);
    string message = "Hi";
    using (var client = new HttpClient { BaseAddress = RecommendationServiceUrl })
    {
        var content = new StringContent("{}", Encoding.UTF8, "application/json");
        var response = await client.PostAsync(
            $"api/recommendations/getrecommendation?test={message}", content);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search