I am using Mirth Connect to automate healthcare data processes, and part of my task involves sending a PDF file over HTTP. I receive the PDF as a base64 string, which I then convert into a file and send as part of an HTTP request. However, I keep receiving a 415 error code with the message that the PDF attachment file type is unsupported. Here is the error response I receive:
{
"StatusCode": 415,
"Message": "Unsupported tempFile8297956747860069271.pdf attachment file",
"Success": false,
"UserMessage": "The tempFile8297956747860069271.pdf attachment is unsupported, please select PDF files only.",
"MemberValidation": null,
"Error": []
}
I’ve checked that the base64 string represents a valid PDF file by converting it manually. I am unsure why the server does not recognize it as a valid PDF. Has anyone encountered this issue, or can anyone suggest what might be causing this error and how to resolve it?
function base64ToBytes(base64String) {
var decoder = java.util.Base64.getDecoder();
return decoder.decode(base64String);
}
function writeBytesToFile(bytes) {
var tempFile = java.io.File.createTempFile("tempFile",".pdf");
var outputStream = new java.io.FileOutputStream(tempFile);
outputStream.write(bytes);
outputStream.close();
return tempFile;
}
function postFileToUrl(file) {
var url = "https://....";
var username = $('USERNAME');
var password = $('PASSWORD');
var httpClient = new org.apache.commons.httpclient.HttpClient();
var postMethod = new org.apache.commons.httpclient.methods.PostMethod(url);
postMethod.setRequestHeader("Username", username);
postMethod.setRequestHeader("Password", password);
var filePart = new org.apache.commons.httpclient.methods.multipart.FilePart("file",file.getName(),file,"application/pdf","UTF-8");
filePart.setContentType("application/pdf");
var multipart = new org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity([filePart],postMethod.getParams());
postMethod.setRequestEntity(multipart);
var responseCode = httpClient.executeMethod(postMethod);
var responseBody = postMethod.getResponseBodyAsString();
if (responseCode == 200) {
logger.info("File uploaded successfully. Response: " + responseBody);
} else {
logger.error("Failed to upload file. Response: " + responseBody);
}
return responseBody;
}
var base64String = $('file');
var bytes = base64ToBytes(base64String);
var tempFile = writeBytesToFile(bytes);
response = postFileToUrl(tempFile)
tempFile.delete();
return response;