Get request to twitch with axios

hello everyone! I want to make a get request on twitch. But I have an error. I write in node js and use axios to make a request.

My code:

import axios from "axios";

axios.post('https://id.twitch.tv/oauth2/token', {
    client_id: 'iaxcs84csy2v5mvzueo69y',
    client_secret: '6uj5ltv3l7uljc5k',
    grant_type: 'client_credentials'
})
  .then(function (response) {
    console.log(response);
})

axios.get('https://api.twitch.tv/helix/search/channels?query=loserfruit', {
    headers: {
      'Client-Id': 'iwnng4cs84csy2v5ueo69y',
      'Authorization': 'Bearer 1n44alq5a9n4e9oz8j4'
}})
  .then(function (response) {
    console.log(response);
})

Error:

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "AxiosError: Request failed with status code 401".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

You don’t have a .catch to catch with your .thens to catch any errors

what am I supposed to do? I just don’t know

import axios from "axios";

axios.post('https://id.twitch.tv/oauth2/token', {
    client_id: 'iaxcs84csy2v5mvzueo69y',
    client_secret: '6uj5ltv3l7uljc5k',
    grant_type: 'client_credentials'
})
  .then(function (response) {
    console.log(response);
})
.catch((e) {
    console.log(e);
});

axios.get('https://api.twitch.tv/helix/search/channels?query=loserfruit', {
    headers: {
      'Client-Id': 'iwnng4cs84csy2v5ueo69y',
      'Authorization': 'Bearer 1n44alq5a9n4e9oz8j4'
}})
  .then(function (response) {
    console.log(response);
})
.catch((e) {
    console.log(e);
});

Is a basic addition to catch and log the error.

You may want to complete a javascript primer before continuing

Thanks ! everything is right!

@BarryCarlyon please can u tell me how to send a message via post request?

Send a message to chat via POST request?

You cannot there is no endpoint for that

How then to do it? Is it possible

Chat messages are only send to channels via connecting to chat.

How to connect to the chat? Is it possible in my version?

Chat is documented here Chat & Chatbots | Twitch Developers

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.