Search code examples
restasp.net-web-api2webapi

How to get request and response from Web Api and save it to the database


I have a method in the Controller class below:

    [HttpGet]
    [ResponseType(typeof(RsContractCustomer))]
    [Route("api/Contract/GetCustomerData/{cardModificationId}")]
    public async Task<IHttpActionResult> GetCustomerData([FromUri] int cardModificationId)
    {
        var jsonIgnoreNullValues = JsonConvert.SerializeObject(await _bs.GetCustomer(cardModificationId), Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore
        });

        JObject jObject = JObject.Parse(jsonIgnoreNullValues);
        return Ok(jObject); //await _bs.GetCustomerData(id));
    }

Now I want to get HttpResponseMessage and save particular part into database. What I should to add in these code or implement middleware to get this?


Solution

  • HttpReponseMessage is received in the client side (Middleware and controllers are on the server side).

    In the client project you can write something like that:

            HttpClient client = new HttpClient();
    
            HttpResponseMessage response = await client.GetAsync("http://localhost:8080/api/contract/getcustomerdata/1");
    
            var customer = await response.Content.ReadAsAsync<RsContractCustomer>();
    
            // Save to DB