SamyCode
SamyCode

Reputation: 1018

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.

Upvotes: 3

Views: 3951

Answers (1)

Nkosi
Nkosi

Reputation: 247123

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.
}

Upvotes: 3

Related Questions