arsu4ka
arsu4ka

Reputation: 13

WebSocket Error: server rejected WebSocket connection: HTTP 403

There's this site https://www.dextools.io/ which basically displays information about different crypto coins. It also has websocket endpoint: wss://ws.dextools.io/ that can provide real-time deals against certain coin. My goal is to connect to this endpoint via NodeJS to retrieve data about deals in real-time. I intercepted the websocket handshake request that browser sends when visiting https://www.dextools.io/ (Inspect -> Network -> WS tab). I tried to establish the same websocket connection via NodeJS using ws lib. Here's the code that I tried:

import WebSocket from "ws";
import * as crypto from "crypto";

const url = "wss://ws.dextools.io";
const ws = new WebSocket(url, {
    headers: {
        'Pragma': 'no-cache',
        'Origin': 'chrome://newtab',
        'Accept-Language': 'en-US,en;q=0.9',
        'Sec-WebSocket-Key': crypto.randomBytes(16).toString('base64'),
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
        'Upgrade': 'websocket',
        'Cache-Control': 'no-cache',
        'Connection': 'Upgrade',
        'Sec-WebSocket-Version': '13',
        'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits'
    }
});

When I run it I get the following error: Error: Unexpected server response: 403 which basically means that server rejected handshake request. However, If I execute the same block of code in Chrome developer console (using WebSocket browser API) - I get no error and connection establishes successfully. The problem isn't with cookies and autorization headers because they're being sent after connection is established.
What am I missing here? How to make it work?

Thanks for you attention!

Upvotes: 1

Views: 2871

Answers (1)

Sergy
Sergy

Reputation: 1

Probably you miss a coockie. $ curl -I https://www.dextools.io HTTP/2 403 date: Fri, 12 Jan 2024 13:39:28 GMT content-type: text/html; charset=UTF-8 {skipped} referrer-policy: same-origin x-frame-options: SAMEORIGIN {skipped} set-cookie: __cf_bm=jiZr1.iD14zSdlq3TDmxUQvZTxH2JTOsuKr9STDjnq8-1705066768-1-Ac4Klcp8MicpEYiED2SRMIZD8esaChu3QnUpz06Ko0tihhUGrWQ2W+nTzwRz9Jlyc/SEXl31IxjDVXshcexX+dw=; path=/; expires=Fri, 12-Jan-24 14:09:28 GMT; domain=.dextools.io; HttpOnly; Secure; SameSite=None {skipped}

Upvotes: 0

Related Questions