The following method is intended to accept an Http method and url, execute the method against the url, and return the resulting response to the caller. It returns ActionResult because there are error conditions that need to be handled.
At present, the method only tells the caller whether the call succeeded or not, it doesn't return details about the response from the downstream server. I would like the caller to receive the entire response (including status code, response body, etc) from the call.
How can I convert the HttpResponseMessage to something appropriate to return via ActionResult?
[HttpGet(@"{method}")]
public async Task<IActionResult> RelayRequest(string method, [FromQuery] string url)
{
var httpMethod = new HttpMethod(method);
Uri uri;
try
{
uri = new Uri(url);
}
catch (Exception e)
{
return BadRequest("Bad URL supplied: " + e.Message);
}
var request = new HttpRequestMessage(httpMethod, uri);
try
{
var response = await _httpClient.SendAsync(request);
// WANT TO RETURN (ActionResult)response HERE! <<<<<<<<<<
if (response.IsSuccessStatusCode)
{
return Ok();
}
return BadRequest(response);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}