question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Node-Fetch not returning json or returning GunZip from SOAP api

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
waldothedevelopercommented, Oct 30, 2019

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!

3reactions
Richienbcommented, Oct 30, 2019

@waldothedeveloper Try res.text instead of res.json

Read more comments on GitHub >

github_iconTop Results From Across the Web

Decompressing gzip response body - node.js - Stack Overflow
Trying to decompress a gzip response body using zlib like this: fetch(url, { method: "POST", headers: { "x-api-token": "***" } }). then(res => ......
Read more >
soap - npm
A SOAP client and server for node.js. This module lets you connect to web services using SOAP. It also provides a server that...
Read more >
Copy and transform data from and to a REST endpoint - Azure ...
The REST API returns response in the following structure, in which case next page's URL is represented in paging.next. JSON Copy. { "data" ......
Read more >
Accept-Encoding - HTTP - MDN Web Docs
The Accept-Encoding request HTTP header indicates the content encoding (usually a compression algorithm) that the client can understand.
Read more >
Error Responses - Amazon Simple Storage Service
Error Code Description HTTP Status Code AccessControlListNotSupported The bucket does not allow ACLs. 400 Bad Request AccessDenied Access Denied 403 Forbidden BucketAlreadyOwnedByYou 409 Conflict (in all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found