Square Node SDK Post Requests return error 'Expected "{" (line 1, character 1)'

Hello, I’ve been getting the following error since Sunday when using the Node SDK. It seems like this is only happening on requests which result in a PUT or POST request under the hood as calls like listCards or listLocations are working, however searchTeamMembers or obtainToken are throwing the following error:

STATUS 400

{
   code: 'BAD_REQUEST',
   detail: 'Expected "{" (line 1, character 1)',
   category: 'INVALID_REQUEST_ERROR'
}

An example of a call through the SDK which is currently failing:

client.teamApi.searchTeamMembers({})

Further notes:
After checking the API logs I noticed that when the POST requests started failing the requests which the SDK was actually sending changed from an object to a string

Failed Request Example: (search catalog objects)

content-length: 42
content-type: application/json
square-version: 2021-08-18
accept: application/json
user-agent: Square-TypeScript-SDK/13.1.0


"{\"query\":{\"filter\":{}},\"limit\":25}"

Successful Request example from a few days ago:

User-Agent: Square-TypeScript-SDK/13.1.0
Accept: application/json
Content-Length: 198
Square-Version: 2021-08-18
Content-Type: application/json


 {
  "object_types": [
    "ITEM"
  ],
  "include_deleted_objects": false,
  "include_related_objects": false,
  "query": {
    "exact_query": {
      "attribute_name": "image_id",
      "attribute_value": "DX2PPNM7OTCQKVOM3RNYZEUX"
    }
  },
  "limit": 1
}

We haven’t been able to replicate this behavior. Would you mind providing the snippet of code that you are getting the error with?

I am fortunately no longer getting this error, but it was in every SDK call which resulted in a POST or PUT request (example client.teamApi.searchTeamMembers({})) . Think it may have been an internal dependency issue which was causing it - I updated my dependencies today and everything seems to be working. (Though doing the same thing yesterday was resulting in the same error, so I am a bit concerned of a potential repeat situation)

I take it back, it seems like this is still coming up when I run Square locally.

A call that’s failing me right now:

const {
  result: { accessToken, refreshToken },
} = await oAuthApi.obtainToken({
  grantType: 'refresh_token',
  clientId: process.env.SQUARE_APP_ID as string,
  clientSecret: process.env.SQUARE_APP_SECRET as string,
  refreshToken: prevRefreshToken,
});

And the full error message:

err =>  ApiError: Response status code was not ok: 400.
    at /Users/jakeengel/code/fryde/fryde-app/node_modules/@apimatic/core/lib/http/requestBuilder.js:360:23
    at DefaultRequestBuilder.<anonymous> (/Users/jakeengel/code/fryde/fryde-app/node_modules/@apimatic/core/lib/http/requestBuilder.js:172:50)
    at step (/Users/jakeengel/code/fryde/fryde-app/node_modules/tslib/tslib.js:143:27)
    at Object.next (/Users/jakeengel/code/fryde/fryde-app/node_modules/tslib/tslib.js:124:57)
    at fulfilled (/Users/jakeengel/code/fryde/fryde-app/node_modules/tslib/tslib.js:114:62)
    at processTicksAndRejections (internal/process/task_queues.js:95:5) {
  request: {
    method: 'POST',
    url: 'https://connect.squareupsandbox.com/oauth2/token',
    headers: {
      'user-agent': 'Square-TypeScript-SDK/13.1.0',
      'content-type': 'application/json',
      'Square-Version': '2021-08-18',
      accept: 'application/json'
    },
    body: {
      type: 'text',
      content: '{"client_id":"sandbox-sq0idb-***","client_secret":"sandbox-***,"grant_type":"refresh_token","refresh_token":"***"}'
    }
  },
  statusCode: 400,
  headers: {
    'access-control-allow-headers': 'Content-Type, Authorization, Accept, X-Allow-Cookies, X-Block-Cookies',
    'access-control-expose-headers': 'Link',
    'content-type': 'application/json',
    vary: 'Origin, Accept-Encoding',
    'x-content-type-options': 'nosniff',
    'x-download-options': 'noopen',
    'x-frame-options': 'DENY',
    'x-permitted-cross-domain-policies': 'none',
    'x-request-id': 'Cgh0StVodOwmyBABGBAiCQjmk8Kh7O3yAg',
    'x-xss-protection': '1; mode=block',
    date: 'Tue, 07 Sep 2021 21:51:21 GMT',
    'content-length': '85',
    'strict-transport-security': 'max-age=631152000; includeSubDomains; preload',
    connection: 'close'
  },
  body: '{\n' +
    '  "message": "error parsing JSON; expected JSON object",\n' +
    '  "type": "bad_request"\n' +
    '}\n',
  result: [Object: null prototype] {
    message: 'error parsing JSON; expected JSON object',
    type: 'bad_request'
  },
  errors: [
    {
      category: 'V1_ERROR',
      code: 'bad_request',
      detail: 'error parsing JSON; expected JSON object',
      field: undefined
    }
  ]
}

What are the dependencies you have in your package.json?

"dependencies": {
    "@googlemaps/google-maps-services-js": "^3.1.16",
    "@sendgrid/mail": "^7.4.6",
    "@slack/web-api": "^6.3.0",
    "apollo-server-express": "^3.3.0",
    "aws-sdk": "^2.981.0",
    "axios": "^0.21.4",
    "bcryptjs": "^2.4.3",
    "class-validator": "^0.13.1",
    "colors": "^1.4.0",
    "date-fns": "^2.23.0",
    "date-fns-tz": "^1.1.4",
    "express": "^4.17.1",
    "generate-password": "^1.6.1",
    "google-auth-library": "^7.8.0",
    "graphql": "^15.5.2",
    "graphql-type-json": "^0.3.2",
    "jimp": "^0.16.1",
    "jsonwebtoken": "^8.5.1",
    "pluralize": "^8.0.0",
    "qrcode": "^1.4.4",
    "sqs-consumer": "^5.6.0",
    "square": "13.1.0",
    "twilio": "^3.67.1",
    "type-graphql": "^1.1.0",
    "typedi": "^0.8.0",
    "typeorm": "^0.2.37",
    "uuid": "^8.3.0"
  }

We created a test app with those dependencies explicitly, and we could not reproduce this issue. Is there anything else that your app is doing that may be causing this?

I am unable to replicate it anymore as well. Either way, I implemented a retry with axios whenever this fails in the future.

In terms of my setup, the only weird thing would be that this application runs in a lerna + yarn-workspaces monorepo

Okay, please let us know if you run into this again with any additional information. :slightly_smiling_face: