0

There are a number of similar questions regarding extracting the content of an HttpResponseMessage, but most are looking for JSON or text.

I have an API in c# that calls another API which returns a Razor page. When I call the second API from Postman, it returns nicely formatted HTML that could be displayed in a browser right away. All I want my API to do (in this instance) is act as a middleman between the browser and the second API.

            var client = new HttpClient();

            const string uri = "Second API";

            var formData = new Dictionary<string, string>
            {
                // Data being sent to second API.
                {"path", "/somehwere/somefile.extension"}
            };
            var encodedFormData = new FormUrlEncodedContent(formData);
            
            var response = await client.PostAsync(uri, encodedFormData);

Now I have the response. If I do the following:

var test = await response.Content.ReadAsStringAsync();

The result is a string with various formatting characters such as "/n". This is not valid HTML and cannot be set as the src of an IFrame in the client browser (which is ultimately what I want to do).

I don't have an issue returning the data that I have extracted to the initial caller, my issue is with formatting.

How do I return valid HTML when I know that the second API is returning it (because Postman displays the html nicely, and because the browser can make the request and display it properly)?

6
  • What is the Content-Type header of the response? Commented Jul 30, 2021 at 17:53
  • @NoahStahl The response that my API recieves from the second api is "text/html". This would as well be why the response is able to be displayed in a browser and in postman. This is also why I think the issue is with how I am accessing the content. Clearly "ReadAsStringAsync()" isn't doing the trick. Commented Jul 30, 2021 at 17:57
  • there's some answers here: stackoverflow.com/questions/26822277/… So just create a page that is served up in the iframe have that controller return the HTML. Commented Jul 30, 2021 at 17:59
  • @pcalkins Thanks, but the answers on that page all presume I have valid HTML like "<p>something</p>". That's my problem, I effectively just want to play the middleman and re-route the valid HTML returned from the second API to the caller, but in being the middleman I ruin the HTML. Accessing it as a string results in invalid HTML with formatting characters like "/n". Commented Jul 30, 2021 at 18:03
  • maybe Ajax is the answer here... just return/append HTML datatype to the iframe? (jQuery's $.get or $.post will parse header from content...) Commented Jul 30, 2021 at 18:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.