Question David Losiewicz · Apr 1, 2020

%CSP.Page - proper way to change ContentType to display text error or return expected pdf content

I want to create a CSP page that returns a pdf. The pdf is identified by a pdftoken parameter.

My goal is to validate the token and return the pdf (ContentType=”application/pdf”) if the token is good and return a text error message "Bad Token" (ContentType=”text/plain”) if the token is missing or bad.

My issue seems to be associated with ContentType. I can define one ContentType that works for the pdf or the error message. I am unable to change the ContentType.

Any assistance is appreciated.

ClassMethod OnPage() As %Status { set pdfToken=$get(%request.Data("pdfToken",1)) if pdfToken="" { set PDFError("ErrorCode")=400 set PDFError("ErrorText")="missing pdftoken" set %response.ContentType="text/plain" set %response.Status =”400 Bad Request” quit %response.Status }

//token passed – serve the pdf set %response.ContentType="application/pdf" set PDFFileName=$piece(^TokenData(pdfToken),"^",1) set stream=##class(%FileBinaryStream).%New() do stream.LinkToFile(PDFFileName) do stream.OutputToDevice() quit $$$OK }

Comments

Alexander Koblov · Apr 1, 2020

David, you should set ContentType in OnPreHTTP method.

0
David Losiewicz  Apr 1, 2020 to Alexander Koblov

Thank you for the quick reply. 

For Clarity....

I should create an OnPreHTTP( ) method that tests my parameter and set the ContentType accordingly.

Pseudo code...

OnPreHTTP()  as %Boolean 

{   

    set pdfToken=$get(%request.Data("pdfToken",1))
    if (pdfToken  is valid)   set      set %response.ContentType="application/pdf"

    else  set %response.ContentType="text/plain"

    quit 1

}

0