0

I need to call this method in MVC controller and pass the UpdateRequest object as json format. how I can do that?

    [HttpPost]
    [Route("updatecertificate")]
    public void updatecertificate([FromBody] UpdateRequest certificatereviewed)
    {
       loansRepository.updatecertificate(certificatereviewed.Id, certificatereviewed.CertificateReview);
    }

and this is the input class:

 public class UpdateRequest {
    public int Id { get; set; }
    public bool CertificateReview { get; set;}
}

this is how I call and send separate variable but now I like to send the class object in json format.

  private async Task UpdateFundingCertificateReviewed(int id, bool fundingCertificateReviewed)
    {
        await httpClient.PostAsync(string.Format("{0}/{1}", LoanApiBaseUrlValue, updatecertificate),null);
    }
nakisa
  • 89
  • 1
  • 2
  • 6

3 Answers3

0

If you want to transform an object into a JSON string, see this question: Turn C# object into a JSON string in .NET 4 var json = new JavaScriptSerializer().Serialize(obj);

Is this what you are after or do you want to know how to construct the http request with a JSON object in the body?

Community
  • 1
  • 1
David Colwell
  • 2,450
  • 20
  • 31
0

I personally like Newtonsoft.Json to serialize the object.

private async Task UpdateFundingCertificateReviewed
        (int id, bool fundingCertificateReviewed)
{
    using (var client = new HttpClient())
    {
        var url = string.Format("{0}/{1}", LoanApiBaseUrlValue, updatecertificate);
        var updateRequest = new UpdateRequest { Id = 1, CertificateReview = true};
        var data = JsonConvert.SerializeObject(updateRequest);
        await client.PostAsync(url, data);
    }
}

FYI: Async without return is not a good practice. However, it is out of the original question.

Win
  • 61,100
  • 13
  • 102
  • 181
0

your questionis not very clear, what is the outcome that you expect ? If you want to POST an request with JSON body you can check the @Win comment, however if you want to make an Response from the Api to the MVC project you should do a bit more steps tough. :))

Petar Minev
  • 498
  • 1
  • 5
  • 10