Skip to content

JSON not supported by fetch #71

@ramoriusvt

Description

@ramoriusvt

I'm having a problem with the node-red-contrib-salesforce that depends on this in that when SF returns anything other than an OK response, I can't see the returned message.

It appears that the problem is in the res.text() and res.json() functionality. Both functions return Promises, and therefore no contents are getting assigned. The example from the node-fetch github (https://github.com/node-fetch/node-fetch/blob/main/example.js) shows:

// Plain text or HTML
const response = await fetch('https://github.com/');
const body = await response.text();

console.log(body);

// JSON
const response = await fetch('https://github.com/');
const json = await response.json();

console.log(json);

<It is immensely frustrating (to have to resort to Postman) to get an integration working with Salesforce.>

I fixed the issue with the following:

async function unsucessfullResponseCheck(res, self, opts) {
  // Only interested when stuff went wrong
  if (res.ok) {
    return res;
  }

  const e = new Error();
  e.statusCode = res.status;
  const body = util.isJsonResponse(res) ? await res.json() : await res.text();
...

<namely, putting 'async' before the function definition, and 'await ' before the assignments to body.>

I also addressed the res.txt() error previously identified.

Please let me know if there're any questions or if there is anything else I can do.

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions