Node-slack-sdk: Error: Headers User-Agent forbidden

Created on 18 Oct 2018  路  4Comments  路  Source: slackapi/node-slack-sdk

Description

Executing WebClient.chat.postMessage in my environment causes the following error.

  console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Headers User-Agent forbidden
        at dispatchError (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:65:19)
        at validCORSPreflightHeaders (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:102:5)
        at Request.preflightClient.on.resp (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:380:12)
        at Request.emit (events.js:182:13)
        at Request.onRequestResponse (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/request/request.js:1066:10)
        at ClientRequest.emit (events.js:182:13)
        at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:546:21)
        at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
        at TLSSocket.socketOnData (_http_client.js:432:20)
        at TLSSocket.emit (events.js:182:13) undefined

I ran the following code using Jest.

import { WebAPICallResult, WebClient } from "@slack/client";

describe("SlackNotifier", () => {
  it("should send message to Slack's channel", async () => {
    const client = new WebClient("My Token");
    const channel = "#my-channel";

    const message = "hello";

    const params = {
      channel: channel,
      text: message
    };

    await client.chat
      .postMessage(params)
      .then((result: WebAPICallResult) => {
        return Promise.resolve(result);
      })
      .catch((error: Error) => {
        return Promise.reject(error);
      });
  });
});

What type of issue is this? (place an x in one of the [ ])

  • [x] bug
  • [ ] enhancement (feature request)
  • [ ] question
  • [ ] documentation related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x] I've read and agree to the Code of Conduct.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Reproducible in:

@slack/client version: 4.8.0

node version: v10.2.1

OS version(s): macOS High Sierra 10.13.6

Steps to reproduce:

  1. Execute the code written in Description.

Expected result:

Message is sent to my channel.

Actual result:

The following error occurs.

  console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Headers User-Agent forbidden
        at dispatchError (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:65:19)
        at validCORSPreflightHeaders (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:102:5)
        at Request.preflightClient.on.resp (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:380:12)
        at Request.emit (events.js:182:13)
        at Request.onRequestResponse (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/request/request.js:1066:10)
        at ClientRequest.emit (events.js:182:13)
        at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:546:21)
        at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
        at TLSSocket.socketOnData (_http_client.js:432:20)
        at TLSSocket.emit (events.js:182:13) undefined

Attachments:

I am confirming that this code works on @slack/client 4.3.1.

other information
  • I am using TypeScript 3.1.3
  • I am using Jest 23.6.0
needs info question

Most helpful comment

@aoberoi Succeeded by setting testEnvironment to Node as in this article.

:jest.config.json { "collectCoverageFrom": [ "src/**/*.{ts,tsx}" ], "moduleFileExtensions": [ "ts", "tsx", "js", "jsx", "json", "node" ], "moduleNameMapper": { "^src/(.*)$": "<rootDir>/src/$1" }, "roots": [ "test" ], "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|jsx|ts|tsx)$", "transform": { "^.+\\.tsx?$": "ts-jest" }, "verbose": true, "testEnvironment": "node" }

Apparently it was caused by js-dom being used by default of Jest.

Thank you, this problem has been solved馃憤

All 4 comments

@keitakn are you running this code in the front-end of a web application? this package doesn't officially support browser execution, but it has worked for some people. i can try to help, but the issue might just end up being that you aren't using the right package for your use case.

the error's stack trace indicates that its being thrown in the code for js-dom. perhaps js-dom does not support setting the User-Agent header for outgoing HTTP requests. that is a capability this package needs, so is it possible for you to remove js-dom from this part of your code?

@aoberoi I am running this code from a Mac terminal.

import { WebAPICallResult, WebClient } from "@slack/client";

describe("SlackNotifier", () => {
  it("should send message to Slack's channel", async () => {
    const client = new WebClient("My Token");
    const channel = "#my-channel";

    const message = "hello";

    const params = {
      channel: channel,
      text: message
    };

    await client.chat
      .postMessage(params)
      .then((result: WebAPICallResult) => {
        return Promise.resolve(result);
      })
      .catch((error: Error) => {
        return Promise.reject(error);
      });
  });
});

When this code was executed with Jest, I got an error that I reported.

https://travis-ci.org/nekonomokochan/aws-lambda-node-logger/builds/443031141?utm_source=github_status&utm_medium=notification

@aoberoi Succeeded by setting testEnvironment to Node as in this article.

:jest.config.json { "collectCoverageFrom": [ "src/**/*.{ts,tsx}" ], "moduleFileExtensions": [ "ts", "tsx", "js", "jsx", "json", "node" ], "moduleNameMapper": { "^src/(.*)$": "<rootDir>/src/$1" }, "roots": [ "test" ], "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|jsx|ts|tsx)$", "transform": { "^.+\\.tsx?$": "ts-jest" }, "verbose": true, "testEnvironment": "node" }

Apparently it was caused by js-dom being used by default of Jest.

Thank you, this problem has been solved馃憤

I think this issue can be closed because it was solved by @keitakn

Was this page helpful?
0 / 5 - 0 ratings