locked
How to do a post call and passing a parameter RRS feed

  • Question

  • User-930352495 posted

    Hello everyone,

    i have this code in my mvc controller:

    using (var client = new HttpClient())
             {
                
                 HttpContent content = new StringContent("array_of_named_parameter"Encoding.UTF8, "application/json");
                 client.BaseAddress = new Uri(baseAddress);
                 var result = await client.PostAsync("Controller1/Action1",content);
     
             }

    in my service i have :

    [HttpPost("Action1")]
          public async Task<IActionResultAction1([FromBodystring[] namedparameter)

    but i can get the service, i obtain always, status code 400 bad request.

    Someone can help me?

    thanks

    Wednesday, June 12, 2019 3:37 PM

Answers

  • User-854763662 posted

    Hi first100 ,

    The 400 Bad Request error is an HTTP status code that means that the request you sent to the website server, often something simple like a request to load a web page, was somehow incorrect or corrupted and the server couldn't understand it.

    The 400 Bad Request error is often caused by entering or pasting the wrong URL in the address window but there are some other relatively common causes as well.

    Make the following modification , remove the route template in HTTP[Verb] attribute and use JsonConvert.SerializeObject on the content of HttpContent

    using (var client = new HttpClient())
    {
          var obj = new string[] { "value1", "value2" };
          var json = JsonConvert.SerializeObject(obj);
          HttpContent content = new StringContent(json , Encoding.UTF8, "application/json");
          client.BaseAddress = new Uri("http://localhost:58038/");
          var result = await client.PostAsync("Controller1/Action1", content);
    }

    [HttpPost]
    public async Task<IActionResult> Action1([FromBody] string[] namedparameter)
    {
    return Json(namedparameter);
    }

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 13, 2019 9:24 AM

All replies

  • User-854763662 posted

    Hi first100 ,

    The 400 Bad Request error is an HTTP status code that means that the request you sent to the website server, often something simple like a request to load a web page, was somehow incorrect or corrupted and the server couldn't understand it.

    The 400 Bad Request error is often caused by entering or pasting the wrong URL in the address window but there are some other relatively common causes as well.

    Make the following modification , remove the route template in HTTP[Verb] attribute and use JsonConvert.SerializeObject on the content of HttpContent

    using (var client = new HttpClient())
    {
          var obj = new string[] { "value1", "value2" };
          var json = JsonConvert.SerializeObject(obj);
          HttpContent content = new StringContent(json , Encoding.UTF8, "application/json");
          client.BaseAddress = new Uri("http://localhost:58038/");
          var result = await client.PostAsync("Controller1/Action1", content);
    }

    [HttpPost]
    public async Task<IActionResult> Action1([FromBody] string[] namedparameter)
    {
    return Json(namedparameter);
    }

    Best Regards ,

    Sherry

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 13, 2019 9:24 AM
  • User-930352495 posted

    Thanks!

    Thursday, June 13, 2019 3:20 PM