0

My REST API returns a PDF document in bytes and I need to call that API and show the PDF document on the ASP page for previewing to the user.

I tried

Response.Write HttpReq.responseBody

but it's writing some unreadable text on the page. The httpReq is my object through which I am calling the REST API.

Response of REST API:

Request.CreateResponse(HttpStatusCode.OK, pdfStream, MediaTypeHeaderValue.Parse("application/pdf"))
user692942
  • 16,398
  • 7
  • 76
  • 175
  • That is because `Response.Write()` writes text in the current `CodePage` back to the browser, if you want to send back binary data use `Response.BinaryWrite()`. – user692942 Feb 17 '17 at 07:18

2 Answers2

1

In Classic ASP, Response.Write() is used to send textual data back to the browser using the CodePage and Charset properties defined on the Response object (by default this is inherited from the current Session and by extension the IIS Server Configuration).

To send binary data back to the browser use Response.BinaryWrite().

Here is a quick example (snippet based off you already having the binary from httpReq.ResponseBody);

<%
Response.ContentType = "application/pdf"
'Make sure nothing in the Response buffer.
Call Response.Clear()
'Force the browser to display instead of bringing up the download dialog.
Call Response.AddHeader("Content-Disposition", "inline;filename=somepdf.pdf")
'Write binary from the xhr responses body.
Call Response.BinaryWrite(httpReq.ResponseBody)
%>

Ideally, when using a REST API via an XHR (or any URL for that matter) you should be checking the httpReq.Status to allow you to handle any errors separately to returning the binary, even set a different content-type if there is an error.

You could restructure the above example;

<%
'Make sure nothing in the Response buffer.
Call Response.Clear()
'Check we have a valid status returned from the XHR.
If httpReq.Status = 200 Then
  Response.ContentType = "application/pdf"
  'Force the browser to display instead of bringing up the download dialog.
  Call Response.AddHeader("Content-Disposition", "inline;filename=somepdf.pdf")
  'Write binary from the xhr responses body.
  Call Response.BinaryWrite(httpReq.ResponseBody)
Else
  'Set Content-Type to HTML and return a relevant error message.
  Response.ContentType = "text/html"
  '...
End If
%>

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • While using the Response.BinaryWrite() its writing some strange characters in browser, do have a way to convert this Binary stream to pdf file and write it on browser ? Thanks. – Prateek Mishra Feb 17 '17 at 08:23
  • @PrateekMishra The *"strange characters"* are the textual representation of the binary for some reason, the binary isn't being seen as a valid PDF. Here are a couple of things to try, make sure to call `Response.Clear()` before `Response.BinaryWrite()` to avoid rogue characters being passed to the browser before the binary and make sure the `Response.ContentType` property is set to an appropriate PDF mime-type. – user692942 Feb 17 '17 at 10:05
1

You'll have to define the content type of the response as PDF:

Response.ContentType = "application/pdf"

Then write the binary data to the response:

Response.BinaryWrite(httpReq.ResponseBody)

Full example:

url = "http://yourURL"

Set httpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
httpReq.Open "GET", url, False
httpReq.Send

If httpReq.Status = "200" Then
    Response.ContentType = "application/pdf"
    Response.BinaryWrite(httpReq.ResponseBody)
Else
    ' Display an error message
    Response.Write("Error")
End If
krlzlx
  • 5,752
  • 14
  • 47
  • 55
  • In the full example I would be checking `If httpReq.Status = 200 Then` before returning the binary so any errors can be handled independently, maybe even switch the content-type. – user692942 Feb 17 '17 at 10:34
  • Thanks, good advice for the response status. For the content-type, I don't think that's needed if the REST API call is supposed to return a binary. – krlzlx Feb 17 '17 at 10:47
  • Technically the `Content-Type` is never needed because the Browser can infer it, doesn't mean you shouldn't set it explicitly though. – user692942 Feb 17 '17 at 10:49
  • If you have in server headers: `X-Content-Type-Options=nosniff`, the browser will not do a MIME-type sniffing. – krlzlx Feb 17 '17 at 10:52
  • True, not the default on IIS though. But, all the more reason to explicitly set the `Content-Type` header. – user692942 Feb 17 '17 at 10:53