binarythistle / S04E03---.NET-Microservices-Course-

Code for the Introduction to .NET Microservices
592 stars 329 forks source link

POST request to CommandsService from PlatformService using HttpClient class failed with 404 status code #9

Closed quachhengtony closed 2 years ago

quachhengtony commented 2 years ago

PlatformService container's logs:

{"EventId":100,"LogLevel":"Information","Category":"System.Net.Http.HttpClient.ICommandDataClient.LogicalHandler","Message":"Start processing HTTP request POST http://commands-clusterip-srv/api/commands/platforms/","State":{"Message":"Start processing HTTP request POST http://commands-clusterip-srv/api/commands/platforms/","HttpMethod":"POST","Uri":"http://commands-clusterip-srv/api/commands/platforms/","{OriginalFormat}":"Start processing HTTP request {HttpMethod} {Uri}"}}

{"EventId":100,"LogLevel":"Information","Category":"System.Net.Http.HttpClient.ICommandDataClient.ClientHandler","Message":"Sending HTTP request POST http://commands-clusterip-srv/api/commands/platforms/","State":{"Message":"Sending HTTP request POST http://commands-clusterip-srv/api/commands/platforms/","HttpMethod":"POST","Uri":"http://commands-clusterip-srv/api/commands/platforms/","{OriginalFormat}":"Sending HTTP request {HttpMethod} {Uri}"}}

{"EventId":101,"LogLevel":"Information","Category":"System.Net.Http.HttpClient.ICommandDataClient.ClientHandler","Message":"Received HTTP response headers after 90.6421ms - 404","State":{"Message":"Received HTTP response headers after 90.6421ms - 404","ElapsedMilliseconds":90.6421,"StatusCode":404,"{OriginalFormat}":"Received HTTP response headers after {ElapsedMilliseconds}ms - {StatusCode}"}}

{"EventId":101,"LogLevel":"Information","Category":"System.Net.Http.HttpClient.ICommandDataClient.LogicalHandler","Message":"End processing HTTP request after 103.0619ms - 404","State":{"Message":"End processing HTTP request after 103.0619ms - 404","ElapsedMilliseconds":103.0619,"StatusCode":404,"{OriginalFormat}":"End processing HTTP request after {ElapsedMilliseconds}ms - {StatusCode}"}}

Exception Caught!
Sync POST to CommandsService failed!
Message :Response status code does not indicate success: 404 (Not Found). 

SendPlatformToCommand:

public async Task SendPlatformToCommand(PlatformReadDto platform)
        {
            try
            {
                var httpContent = new StringContent(
                JsonSerializer.Serialize(platform),
                Encoding.UTF8,
                "application/json"
                );

                // var response = await _httpClient.PostAsync($"{_configuration["CommandService"]}", httpContent);

                // if (response.IsSuccessStatusCode)
                // {
                //     Console.WriteLine("Sync POST to CommandsService success!");
                // }
                // else
                // {
                //     Console.WriteLine("Sync POST to CommandsService failed!");
                // }

                HttpResponseMessage response = await _httpClient.PostAsync($"{_configuration["CommandService"]}", httpContent);

                response.EnsureSuccessStatusCode();
                Console.WriteLine("Sync POST to CommandsService success!");

            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Sync POST to CommandsService failed!");
                Console.WriteLine("Message :{0} ", ex.Message);
            }

Can anyone tell me what the problem is? Thank you.

quachhengtony commented 2 years ago

I found the issue, it was the .UseHttpsRedirection().