skip to Main Content

I have two ASP.NET Core Web API projects called app.account and core.account. I the both projects have some testing API endpoint like this.

In app.account:

namespace app.account.Controllers
{
    [ApiController]
    public class SubInteractiveStatementController : ControllerBase
    {
        [HttpPost]
        [Route("account/istatement")]
        public async Task<ActionResult> Test(Test cif)
        {
            return Ok("hello gamma");
        }
    }
}

public class Test
{
    public string cif { get; set; }
}

In core.account:

namespace core.account.Controllers
{
    [ApiController]
    public class CasaDetailController : ControllerBase
    {
        [HttpPost]
        [Route("account/istatement")]
        public async Task<ActionResult> Test(Test cif)
        {
            return Ok("hello gamma");
        }
    }
}

I’m calling the above API endpoint throughout the client side like this:

try {
  CommonResponse commonResponse = new();

  var data_ = JsonConvert.SerializeObject(test);
  var buffer_ = System.Text.Encoding.UTF8.GetBytes(data_);
  var byteContent_ = new ByteArrayContent(buffer_);
  byteContent_.Headers.ContentType = new MediaTypeHeaderValue("application/json");

  //string _urls = "http://localhost:5088/account/istatement";
  string _urls = "http://localhost:5035/account/istatement";
  var responses_ = await _httpClient.PostAsync(_urls, byteContent_);

  if (responses_.StatusCode == HttpStatusCode.OK) {
    Console.WriteLine("[GetPrimeryAccount] Response: Success");
    string body = await responses_.Content.ReadAsStringAsync();
    var rtns = JsonConvert.DeserializeObject < CommonResponse > (body);
  }
} catch (global::System.Exception) {

  throw;
}

http://localhost:5088/account/istatement is the app.account API endpoint and http://localhost:5035/account/istatement is the core.account API endpoint.

When I call the app.account, I’m getting the following error.

{ StatusCode: 400, ReasonPhrase: ‘Bad Request’, Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Wed, 19 Oct 2022 08:43:21 GMT
Server: Kestrel
Transfer-Encoding: chunked
}}

But it’s properly calling the core.account API endpoint.

Both URL’s are correct, I checked both endpoints using Postman, and that worked properly.

What could be the reason for this? Could it be a HTTP version issue? How can I determine the exact reason for this? Please help. Thanks

3

Answers


  1. Regarding the request

    Instead of manually composing the data as byte array content, you can use the StringContent class

    await _httpClient.PostAsync(_urls, new StringContent(data_, Encoding.UTF8, "application/json");
    

    If you want to shorten it even more, there’s a extension

    await _httpClient.PostAsJsonAsync(_urls, data_);
    
    Login or Signup to reply.
  2. What could be the reason for this? Could it be a HTTP version issue?
    How can I determine the exact reason for this?

    Well, you are certainly getting the error because of your [Route("account/istatement")] route. You cannot use multiple route attribute for same action, not matter even if the controller are different even if the projcts are different within the same solution.

    Remember your route attribute should be unique within the solution if you defined it following this fashion [Route("account/istatement")].

    Solution:

    If you comment either of this route attribute from either of the controller it would work. Or you can use the route attribute globally on your controller level.

    Output:

    enter image description here

    Login or Signup to reply.
  3. The problem seems to be in a part that you have not shared with us.
    Please share both of the API project’s startup.cs here. For a hint, If you are using any custom header validation middleware, make sure to pass those header values properly. Otherwise, that may cause to occur 400 Bad Request.

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    
        app.UseMiddleware<YourcCustomHeaderValidationClass>(); // Custom header validation
    
        app.UseRouting();
    }
    

    Compare the both startp.cs files and you will be able to identify the problem there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search