Home > Mobile >  ASP.NET Core Web API send 400 Bad Request for request client
ASP.NET Core Web API send 400 Bad Request for request client

Time:10-19

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 sachith");
        }
    }
}

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

CodePudding user response:

Regarding the request

Instead of manually composing the data as byte array content, you can use the enter image description here

  • Related