Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read application/pdf content type from HttpClient response and return it

I have a Web API which successfully connects to another Web API using HttpClient. A GET method from the second Web API returns an application/pdf content-type.

I can see the content-type of the response at the debugger, using:

var response = await client.GetAsync("{url}")

The question is, how can I read that stream and successfully return it in my own get method?

Preferably, the method type should be IHttpActionResult.

like image 228
SamyCode Avatar asked Dec 06 '25 05:12

SamyCode


1 Answers

You can forward the content on and also get all the necessary details from the response of the other request

public async Task<IHttpActionResult> MyAction() {

    //...code removed for brevity

    var response = await client.GetAsync("{url}");
    if (response.IsSuccessStatusCode) {
        var message = Request.CreateResponse(HttpStatusCode.OK);
        message.Content = response.Content;
        return ResponseMessage(message);
    }

    return BadRequest(); //or some other status response.
}
like image 168
Nkosi Avatar answered Dec 08 '25 19:12

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!