IBM / ibm-cos-sdk-js

ibm-cos-sdk-js
Apache License 2.0
38 stars 20 forks source link

XMLParserError: Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar: { #74

Closed mriedem closed 3 years ago

mriedem commented 3 years ago

I'm using version 1.7.0 and simply trying to list buckets but I get this error:

Error [XMLParserError]: Non-whitespace before first tag.
Line: 0
Column: 1
Char: {
    at error (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/sax/lib/sax.js:651:10)
    at strictFail (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/sax/lib/sax.js:677:7)
    at beginWhiteSpace (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/sax/lib/sax.js:951:7)
    at SAXParser.write (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/sax/lib/sax.js:1006:11)
    at Parser.exports.Parser.Parser.parseString (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/xml2js/lib/parser.js:323:31)
    at Parser.parseString (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/xml2js/lib/parser.js:5:59)
    at NodeXmlParser.parse (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/ibm-cos-sdk/lib/xml/node_parser.js:30:10)
    at Request.extractData (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/ibm-cos-sdk/lib/protocol/rest_xml.js:78:23)
    at Request.callListeners (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/ibm-cos-sdk/lib/sequential_executor.js:105:20)
    at Request.emit (/home/osboxes/ibmq/ibmq-jupyterhub-cos-api/node_modules/ibm-cos-sdk/lib/sequential_executor.js:77:10) {
  message: 'Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar: {',
  code: 'XMLParserError',
  retryable: true,
  time: 2020-09-17T19:44:57.928Z,
  statusCode: 200
}

I'm creating the S3 object like this:

const sdk = require('ibm-cos-sdk')

const { s3: s3Config } = require('./config')
const logger = require('./utils').createLogger('s3')

const cos = new sdk.S3({ ...s3Config, logger })

And calling listBuckets like this:

    s3.listBuckets((err, data) => {
        if (err) return console.error(err)
        return log.info({ data }, 'Got buckets')
    })

Credentials and everything seem OK otherwise I shouldn't be getting a 200 response. But it looks like the issue is the code is trying to parse XML when in fact it should be dealing with JSON, which reading https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/working-with-json.html and https://ibm.github.io/ibm-cos-sdk-js/AWS/S3.html#listBuckets-property I would expect the response body to be properly parsed into a JSON object for javascript.

I'm running within a nodejs v12.16.2 environment.

Is there some way to debug this, like would it help to get the headers off the response object?

mriedem commented 3 years ago

Apparently I was using the wrong endpoint when creating the S3 service object. I was using the endpoint from the service credentials JSON file I downloaded from the IBM COS dashboard for my instance (https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints) but should be using https://s3.us-south.cloud-object-storage.appdomain.cloud.

tqtran7 commented 3 years ago

Yes, I ran into the same issue. Also figured it out the same way you did. The instructions can be a bit more clear.