Taking Base64 Encoded PDF Data from Json Content, Decoding It, and Returning It to Be Displayed or Downloaded in Browser
Example Response Data
{"pdf":"<BASE64_ENCODED_DATA>"}
Displaying in Browser
import groovy.json.JsonSlurper
import java.util.Base64
def slurper = new JsonSlurper()
def parsedJson = slurper.parseText(responseBodyTextFromTargetAPI)
def pdfAsBase64 = parsedJson.data;
responseBodyTextToClient = new String(Base64.decoder.decode(pdfAsBase64), "UTF-8")
responseHeaderMapToClient.put("Content-Type", "application/pdf")
responseHeaderMapToClient.put("Content-Disposition", "inline")
uyarı
Not every web browser may provide this display.
PDF Download Operation
import groovy.json.JsonSlurper
import java.util.Base64
def slurper = new JsonSlurper()
def parsedJson = slurper.parseText(responseBodyTextFromTargetAPI)
def pdfAsBase64 = parsedJson.data;
responseBodyTextToClient = new String(Base64.decoder.decode(pdfAsBase64), "UTF-8")
responseHeaderMapToClient.put("Content-Type", "application/pdf")
responseHeaderMapToClient.put("Content-Disposition", "attachment")
not
In both operations, it should be verified that the PDF content is reflected correctly. This script should be run on the response line (Response Policy).