2

A console application is making a call to an Web API but getting 404 response.

The responseBytes array in client below, when converted to string using System.Text.Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length); returns following HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Not Found</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Not Found</h2>
<hr><p>HTTP Error 404. The requested resource is not found.</p>
</BODY></HTML>

Client call

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(action);//action=http://<my-app>/headcount/comparison/zip
    _logger.InfoFormat("downloading files from {0}", action);
    var response = await client.PostAsync(action, new StringContent(jsonInString, Encoding.UTF8, "application/json"));
    responseBytes = await response.Content.ReadAsByteArrayAsync();
}

Controller/action:

[ApiController]
[Route("[controller]")]
public class HeadcountController : ControllerBase
{

    [HttpPost]
    [Route("comparison/zip")]
    [Consumes("application/json")]
    public IActionResult GetReports(ReportRequest request)
    {
        //action details...never reached
    }
}

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers().AddNewtonsoftJson();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

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

What am I doing wrong?

joym8
  • 4,014
  • 3
  • 50
  • 93

1 Answers1

0

You may need to set the client content type to application/x-www-form-urlencoded.

    client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
Victor Wilson
  • 1,720
  • 1
  • 11
  • 22
  • `HttpClient` does not have `Headers` property. And doing `DefaultRequestHeaders.Add` causes runtime exception https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8 – joym8 Mar 10 '20 at 21:51
  • Hmm. I've more experience with the back end side. We had to set the content type for our front end to get our back end to accept POSTs. Are you able to try with any other libraries? – Victor Wilson Mar 10 '20 at 22:15
  • Maybe you will find this useful: https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request – Victor Wilson Mar 10 '20 at 22:15
  • 1
    My development machine's hosts file was pointing to production API which did not have the controller/action I was calling. Sigghh.... – joym8 Mar 11 '20 at 14:06