When trying to post to the AutoTask SOAP API the result is not XML nor JSON.
This is my code:
const url = "https://webservices14.autotask.net/atservices/1.6/atws.asmx";
const options = {
method: "POST",
headers: {
"Accept-Encoding": "gzip, deflate",
"Content-Length": "741",
Host: "webservices14.autotask.net",
Accept: "application/json",
SOAPAction: "http://autotask.net/ATWS/v1_6/query",
"Content-Type": "text/xml"
},
body:
'<?xml version="1.0" encoding="utf-8"?>\n<soap:Envelope\n xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"\n xmlns:x-si="http://www.w3.org/2001/XMLSchema-instance"\n xmlns:xsd="http://www.w3.org/2001/XMLSchema">\n <soap:Header>\n <AutotaskIntegrations\n xmlns="http://autotask.net/ATWS/v1_6/">\n <IntegrationCode>CZQT7KI35IMIXUTRVAIWCLUOH3A</IntegrationCode>\n </AutotaskIntegrations>\n </soap:Header>\n <soap:Body>\n <query\n xmlns="http://autotask.net/ATWS/v1_6/">\n <sXML>\n <![CDATA[<queryxml><entity>Ticket</entity><query><condition><field>TicketNumber<expression op="equals">T20191003.0019</expression></field></condition></query></queryxml>]]>\n </sXML>\n </query>\n </soap:Body>\n</soap:Envelope>'
};
fetch(url, options)
.then(res => console.log(res))
.catch(err => console.log("err:", err));
Please NOTE that I purposedly removed the _Authorization Header_ for security purposes to avoid exposing private company data.
The result:
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: {
body: Gunzip {
_writeState: [Uint32Array],
_readableState: [ReadableState],
readable: true,
_events: [Object: null prototype],
_eventsCount: 6,
_maxListeners: undefined,
_writableState: [WritableState],
writable: false,
allowHalfOpen: true,
_transformState: [Object],
_hadError: false,
bytesWritten: 0,
_handle: [Zlib],
_outBuffer: <Buffer 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 75 74 66 2d 38 22 3f 3e 3c 73 6f 61 70 3a 45 6e 76 65 6c 6f ... 16334 more bytes>,
_outOffset: 0,
_chunkSize: 16384,
_defaultFlushFlag: 2,
_finishFlushFlag: 2,
_defaultFullFlushFlag: 3,
_info: undefined,
_level: -1,
_strategy: 0
},
disturbed: false,
error: null
},
[Symbol(Response internals)]: {
url: 'https://webservices14.autotask.net/atservices/1.6/atws.asmx',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: 0
}
}
Trying res.json() returns:
Promise { <pending> }
(node:22345) UnhandledPromiseRejectionWarning: FetchError: invalid json response body at https://webservices14.autotask.net/atservices/1.6/atws.asmx reason: Unexpected token < in JSON at position 0
at /Users/myusername/Documents/Sites/conviva-ticket-tracker-system/server/node_modules/node-fetch/lib/index.js:272:32
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:22345) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:22345) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I've tried with the Zlib node library but also no luck.
I'm running out of time and strengths!
Please help!
And btw the Documentation on NPMJS says:
What I'm missing then?
@waldothedeveloper Try res.text instead of res.json
@Richienb Try
res.textinstead ofres.json
I did Richie. Same result.
@waldothedeveloper res.text() should not give you FetchError: invalid json response body error, if the problem is somehow the promise can't be resolved, try res.body.pipe(file) and see what you got in response.
(Also I find it weird you got a UnhandledPromiseRejectionWarning, the rejection should be handled if you did add a catch() clause.
I finally was able with this:
fetch(url, options)
.then(res => res.text())
.then(data => {
const result = convert.xml2js(data);
console.log("result in json: ", result);
})
.catch(err => console.log(err));
Using the npm package xml2json was able to get my data.
But even without this package after res.text() if the promise is resolved I was able to get my data as a string.
thank you everyone for your awesome help!
Most helpful comment
I finally was able with this:
Using the npm package xml2json was able to get my data.
But even without this package after res.text() if the promise is resolved I was able to get my data as a string.
thank you everyone for your awesome help!