I am having an issue when posting to the ups api. That doesn’t matter too much for the issue I am having but just for context. My XML is as follows:
<?xml version="1.0" encoding="ISO-8859-1"?>
<AccessRequest xml:lang="en-US">
<AccessLicenseNumber>licensenumber</AccessLicenseNumber>
<UserId>userid</UserId>
<Password>password</Password>
</AccessRequest>
<?xml version="1.0" encoding="ISO-8859-1"?>
<ShipmentAcceptRequest>
<Request>
<TransactionReference>
<CustomerContext>Customer Comment</CustomerContext>
</TransactionReference>
<RequestAction>ShipAccept</RequestAction>
<RequestOption>1</RequestOption>
</Request>
<ShipmentDigest>//a whole lot of text</ShipmentDigest>
</ShipmentAcceptRequest>
I am aware that I am concatenating the xml documents. The UPS api requires I do so to authenticate. Certainly not the right way to do it but that is besides the point. Can anyone tell me why I am getting an error stating “no XML declaration in the XML document?”
Edit: This is how I am posting the ShipAccept
function shipAccept(digest) {
xml = fs.readFileSync('./xml/response.xml', 'utf8');
xml2js.parseString(xml, (err, result) => {
if (err) {
console.err(err)
}
const resJsonString = JSON.stringify(result, null, 4);
const resJson = JSON.parse(resJsonString);
digest = resJson.ShipmentConfirmResponse.ShipmentDigest.toString();
})
var xmlAcc = "<?xml version="1.0" encoding="ISO-8859-1"?>"
xmlAcc += "n<AccessRequest xml:lang="en-US">"
xmlAcc += `n<AccessLicenseNumber>${licenseId}</AccessLicenseNumber>`
xmlAcc += `n<UserId>${userId}</UserId>`
xmlAcc += `n<Password>${password}</Password>`
xmlAcc += "n</AccessRequest>"
xmlAcc += "n<?xml version="1.0" encoding="ISO-8859-1"?>"
xmlAcc += "n<ShipmentAcceptRequest>"
xmlAcc += "n<Request>"
xmlAcc += "n<TransactionReference>"
xmlAcc += "n<CustomerContext>Customer Comment</CustomerContext>"
xmlAcc += "n</TransactionReference>"
xmlAcc += "n<RequestAction>ShipAccept</RequestAction>"
xmlAcc += "n<RequestOption>1</RequestOption>"
xmlAcc += "n</Request>"
xmlAcc += `n<ShipmentDigest>${digest}</ShipmentDigest>`
xmlAcc += "n</ShipmentAcceptRequest>"
fs.writeFile("xml/Acceptrequest.xml", xmlAcc, function (err) {
if (err) {
console.log(err);
}
console.log("ShipAccept Request file written successfully");
});
var options = {
url: `${upsPath}/ShipAccept`,
method: "POST",
body: xmlReq
};
function callback(error, response, body) {
console.log("callback");
if (!error) {
var cbResponse = (body);
fs.writeFile("xml/responseAccept.xml", cbResponse, function (err) {
if (err) {
console.log(err);
}
console.log("ShipAccept Response file written successfully");
});
} else {
console.log(body);
}
}
request.post(options, callback);
}
shipAccept();