Uploading PDF using POST and unwanted utf-8 encoding
Copy link to clipboard
Copied
Dear Community
My name is Sebastian and this forum has been a valuable read-only resource for me in the past months or so. I am developing folder level javascript tools for Acrobat and - I hope that's OK - XChange Editor which is almost entirely conformant with the API.
Now I am having a problem that is driving me nuts and it is this:
I am trying to oupload an entire PDF along with a couple of parameters to a web service under my control.
The server side is functional and is flawlessly handling file upload forms POSTed from browsers.
The server expects Content-Type: 'multipart/form-data' and requires 'basic' authentication.
The Acrobat API offers several options, none of which works for me out of the box:
doc.submitForm({cUrl:..., cSubmitAs: 'PDF'})
- doesn't allow authentication
Net.HTTP.request ({cUrl:..., cVerb: 'POST', oAuthenticate: {...}})
- doesn't *cooperate* with my server when using oAuthenticate
- requires additional custom headers
So I ended up implementing the request from scratch and successfully tested it against my own server and two online test services. (https://ptsv2.com/, https://httpbin.org/post).
The only problem is:
The message body is utf-8 encoded, because I am constructing it in a string object.
That's no problem for most of the message body but it mangles the pdf's binary data, which becomes a utf-8 encoded version of the ASCII-representation of the pdf's binary data.
I will attach the code below but here I am only quoting the message body:
--224061383921529899882446413168
Content-Disposition:form-data; name="Project"
Project-1
--224061383921529899882446413168
Content-Disposition:form-data; name="FILE1"; filename="x.pdf"
Content-Type: application/pdf
%PDF-1.7
%����
[...]
--224061383921529899882446413168--
The characters below %PDF-1.7 are utf-8 encoded as I was able verify by inspecting the server dump of "FILE1". In fact I can *read* the correct ASCII characters in Notepad++ when I view the file in utf-8 mode. However when I switch to Notepad++'s ANSI-mode I see the actual ASCII characters (or bytes if you will) and they differ from the original PDF.
My question probably is:
How can I provide an unencoded binary stream to Net.HTTP.request?
The way I am doing it now requires a string object which interprets the binary data as ASCII charcters and encodes them as utf-8.
Here comes the relevant code
// PDF stream fetched from a local file
var stmPDF = util.readFileIntoStream("/C/temp/eBoxTest/Att.pdf");
// Request headers
var boundary = "224061383921529899882446413168";
var aHeaders = [
{name:"Content-Type", value: "multipart/form-data;boundary=" +boundary+ "\r\n"},
{name:"Accept", value: "text/javascript,text/html,application/xml,text/xml\r\n"},
{name:"Accept-Encoding",value: "gzip,deflate\r\n"},
{name:"Authorization", value: "Basic " + sAuth64 + "=="}
];
// Request body, including string-converted PDF stream
var strContent = "--" +boundary+ "\r\n"
+ "Content-Disposition:form-data; name=\"Project\""
+ "\r\n\r\n" + "Project-1" + "\r\n"
+ "--" +boundary+ "\r\n"
+ "Content-Disposition:form-data; name=\"FILE1\"; filename=\"x.pdf\"" + "\r\n"
+ "Content-Type: application/pdf" + "\r\n\r\n"
+ util.stringFromStream(stmPDF) + "\r\n"+"--" +boundary+ "--";
// The POST request
Net.HTTP.request ({
cVerb: 'POST',
cURL: cUrl,
aHeaders: aHeaders,
oRequest: util.streamFromString(strContent),
oHandler: {
response: function (response, uri, err) {
if (err != undefined)
console.println("== FAIL ==");
else {
console.println("== SUCC ==");
console.println(SOAP.stringFromStream(response));
}
}
}
});
Thanks a lot!
Copy link to clipboard
Copied
Based on what I've been reading outside of this Acrobat forum, see in your aHeader declaration, if the API that you're you're calling requires to specify "application/octect-stream" header for the request constructor (just stabbing in the dark here, this is all new to me)

