Problems with hitting your API using Node.js

Hey everyone!

Not sure what category is right for it, feel free to re-categorize it. To begin with thanks for help with the Python script doing the same thing as the script in Node that I’m currently writing :clap:t3:. So I’m trying to make the API request to execute the Data Explorer Query receive the response and simply display the number that I get from it. While my Python code is working. It’s here:

# Imports

import requests
import json

# Environment Variables

ENDPOINT = 'https://community.myCompanyName.com/admin/plugins/explorer/queries/73/run'
API_KEY = '<api_key>'
API_USERNAME = '<my_username'

# Core Functions

def send_request(endpoint):

    headers = {'Content-Type': 'multipart/form-data', 'Api-Key': API_KEY, 'Api-Username': API_USERNAME}
    request = requests.post(url = endpoint, headers = headers)
    print("Request Status Code: {}".format(request.status_code))

    # Unprocessed API request response

    response = json.loads(request.text)

    response_columns = response["columns"]
    response_rows = response["rows"]

    new_users = response_rows[0][0]
    active_users = response_rows[0][1]
    new_topics = response_rows[0][2]
    employees_replies = response_rows[0][4]
    external_users_replies = response_rows[0][3] - response_rows[0][4]

    response_text = "🤖 Discourse Last Month Stats\nNew Users: {}\nActive Users: {}\nNew Topics: {}\nEmployees Replies: {}\nExternal Users Replies: {}".format(new_users, active_users, new_topics, employees_replies, external_users_replies)

    return response_text

processed_response = send_request(ENDPOINT)

I tried to port it to Node.js but receiving 301 status code - “Moved Permanently” and it seems to be doing exactly the same what my Python code. Here it is:

const http = require('http');

const options = {
  hostname: 'community.myCompanyNamee.com',
  path: '/admin/plugins/explorer/queries/73/run',
  method: 'POST',
  headers: {
    'Content-Type': 'multipart/form-data',
    'Api-Key': <api_key>,
    'Api-Username': <my_username>
  }
};

var response = 1;

const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`);
  response = res;

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.end();

var parsed_response = JSON.parse(response);

I have no experience in JS so if anyone can take a look at it it would be great :slight_smile:

Figured it out. Here’s the solution code:


const http = require('https');

const options = {
  hostname: 'community.myCompanyName.com',
  path: '/admin/plugins/explorer/queries/73/run',
  method: 'POST',
  headers: {
    accept: 'application/json',
    'Content-Type': 'multipart/form-data',
    'Api-Key': '<api_key>',
    'Api-Username': '<my_username>'
  }
};

var response = 1;

const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`);
  response = res;

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (error) => {
  console.error(error);
});

req.end();

var parsed_response = JSON.parse(response);
console.log(parsed_response);

7 Likes