REST postBody with postFormat XML
-
Christopher Steinbach5 years ago #15698
Hi,
is it possible to do a post request with a XML body? Or ist only JSON and PLAIN supported?
What happens if I set postFormat to XML and pass a XML string to postBody?
Thanks in advance.
Christopher
Chris BouveretHas successfully completed the online course IntroductionHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)Has successfully completed the online course Advanced (320)Has successfully completed the Intermediate CertificationHas successfully completed the Advanced Certification5 years ago #15741::Hi there,
there is a dedicated postFormat for XML so there should be no problem in doing post requests with XML postBody. This is also documented here:
Hope that helps
Christopher Steinbach5 years ago #15911::That’s right. There is a XML postFormat property. But is XML-String accepted for the postBody? The documentation says only JSON:
postBody (optional): a JSON defining the post body. If postBody PLAIN is selected, a string JSON representation of the key/value list will be send to the REST Endpoint
Can you provide an example how to send a post request with XML Body?
Christopher Steinbach5 years ago #15912::That’s right. There is a XML postFormat property. But is XML-String accepted for the postBody? The documentation says only JSON:
postBody (optional): a JSON defining the post body. If postBody PLAIN is selected, a string JSON representation of the key/value list will be send to the REST Endpoint
Can you provide an example how to send a post request with XML Body?
Jennifer HäfnerHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)Has successfully completed the online course Advanced (310)Has successfully completed the online course Advanced (320)Has successfully completed the Intermediate CertificationHas successfully completed the Advanced CertificationSatish1 year ago #35851::Hi Jennifer,
I am using the following code snippet fro xml generation :
var dataToExport = this.getGlobals().getVar(“dataToExport”);
var selectedSerialNumber = this.getGlobals().getVar(“selectedSerialNumber”);
var xmlBase64 = this.getGlobals().getVar(“xmlBase64″);
var inputJSON = ‘{}’;
var that = this;var parsedInput = JSON.parse(inputJSON);
parsedInput = dataToExport;function OBJtoXML(obj) {
var xml = ”;for (var prop in obj) {
if (obj[prop] instanceof Array) {
for (var array in obj[prop]) {
xml += ‘<‘ + prop + ‘>’;
xml += OBJtoXML(new Object(obj[prop][array]));
xml += ‘</’ + prop + ‘>’;
}
} else {
xml += ‘<‘ + prop + ‘>’;
typeof obj[prop] == ‘object’ ? xml += OBJtoXML(new Object(obj[prop])) : xml += obj[prop];
xml += ‘</’ + prop + ‘>’;
}
}xml = xml.replace(/<\/?[0-9]{1,}>/g, ”);
return xml;}
// var output = ‘<?xml version=”1.0″ encoding=”ISO8859-1″ standalone=”no”?>’;console.log(OBJtoXML(parsedInput));
var output = `<?xml version=”1.0″ encoding=”ISO8859-1″ standalone=”no”?>
<Austauschdatei>
<Version>1.0.0</Version>
<!–Version der Austauschdatei–>
<Austauschflag>2</Austauschflag>
<!–Austauschflag zwischen CATS und Sitace–>`;
output += OBJtoXML(parsedInput);
output +=</Austauschdatei>
;that.getGlobals().setVar(“xmlBase64”, btoa(output));
var filename = “austausch_”+selectedSerialNumber+”.xml”;
var pom = document.createElement(‘a’);
var bb = new Blob([output], {type: ‘text/plain;charset=ANSI;’});pom.setAttribute(‘href’, window.URL.createObjectURL(bb));
pom.setAttribute(‘download’, filename);pom.dataset.downloadurl = [‘text/plain;charset=ANSI;’, pom.download, pom.href].join(‘:’);
pom.draggable = true;
pom.classList.add(‘dragout’);pom.click();
Alex KHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)Has successfully completed the online course Advanced (310)Has successfully completed the online course Advanced (320)1 year ago #36254::Is there any possibility to make a POST without even postFormat declared?
In curl this is possible, but trying to create a POST without postFormat results in an error.Reason to this is:
once selecting any kind of “Format”, the Posted File-format is declared preemtively. You cannot change it.
But what happens if the API in the background only allows Images or Datatypes like .xls or .pdf only?
This will 100% always result in ERROR.Maybe implement more ways to post data?
Jennifer HäfnerHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)Has successfully completed the online course Advanced (310)Has successfully completed the online course Advanced (320)Has successfully completed the Intermediate CertificationHas successfully completed the Advanced Certification1 year ago #36570::Hi Satish,
Try to encode your XML in the format ‘window-1252’:
<?xml version="1.0" encoding="windows-1252" standalone="no"?>
If does not work for you, you can try the solution proposed in this thread, with a modified TextEncoder to create the blob: https://stackoverflow.com/questions/44135861/javascript-csv-text-download-in-ansi-windows-1252
However, I have not tested this solution.
Jennifer HäfnerHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)Has successfully completed the online course Advanced (310)Has successfully completed the online course Advanced (320)Has successfully completed the Intermediate CertificationHas successfully completed the Advanced Certification1 year ago #36572::Alex, regarding your comment: it is true that currently, the options for the postFormat are limited and indeed, in some use cases, it won’t be possible to use Simplifier’s REST connector.
In these cases, we have to work with client-side XHR / ajax calls. We are planning to add more functionality to the REST connector in the future, but I cannot tell you more detailed information when the new functionality will be available.
You must be logged in to reply to this topic.