简体   繁体   中英

.NET Core 3.1 WebApi - Passthrough JSON response from another WebApi

I have the following setup:

  • WebApi #1 - Returns JSON via an OkResult class
  • WebApi #2 - Returns JSON via an OkResult class

WebApi #2 needs to call WebApi #1 and simply pass the JSON result back via its OkResult. The issue I have is if I do that, I end up with escaped JSON in the response:

"{\"id\":1,\"key\":\"value\",\"key\":\"value\",\"key\":\"value\",\"key\":\"value\"}"

What is the best way to just passthrough this return?

UPDATE:

Here is the code that makes the API call to WebApi #1:

public async Task<string> Get(string uri)
    {
        try
        {
            string responseBody = await _client.GetStringAsync(uri);
            return responseBody;
        }
        catch (HttpRequestException ex)
        {
            _logger.LogError(ex.Message);
            return null;
        }
    }

And here is the return in WebApi #2:

public async Task<IActionResult> Get(string data)
    {
        var uri = String.Format(Environment.GetEnvironmentVariable("ENDPOINT"), data);
        var result = await _client.Get(uri);
        return Ok(result);
    }

You need to do this:

return Content(result, "application/json");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM