Send a request to the uClassify API using the Node request module

I'm currently working on integrating the uClassify API into my Node project, but I'm encountering some issues with my code. Here's what I have so far:

  const req = JSON.stringify('Hello, my love!'); 
  const options = {
    body: req,
    method: 'POST',
    url: 'https://api.uclassify.com/v1/uClassify/Sentiment/classify',
    headers: {
      'Content-Type': 'application/json',
      Authorization: 'MyKey'
    }
  };

  request(options, (error, response, body) => {
    if (!error) {
      callback(response);
    }
  });

Upon running this code, I receive the following response:

statusCode: 400,
body: "{"statusCode":400,
        "message": "Error converting value \"Hello, my love!\" to
        type 'UClassify.RestClient.TextPayload'. Path '', line 1, position 17."}"
      }"

Despite consultingthe documentation, I'm struggling to find specific instructions for JavaScript implementations and am unsure if I have correctly translated their cURL example into my Node.js request code.

url -X POST -H "Authorization:Token YOUR_READ_API_KEY_HERE" -H "Content-Type: application/json" --data "{\"texts\":[\"I am so happy today\"]}"

Answer №1

When writing your Node.js code, make sure to check the body parameter as it may contain errors (even though it is correct in your cURL request). Make sure that uClassify expects an object with a texts property. Revise the body section of your node.js code like this:

const req = JSON.stringify({ texts: ['Hello, how are you?'] }); 
const options = {
  body: req,
  method: 'POST',
  url: 'https://api.uclassify.com/v1/uClassify/Sentiment/classify',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'MyKey'
  }
};

request(options, (error, response, body) => {
  if (!error) {
    callback(response);
  }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Rotating images on a canvas

We're currently implementing Ionic and Angular in our project. One issue we are facing is regarding image rotation on canvas. When we click on an image, the rotation works perfectly if it's a jpg file. However, when we pass a base64 image, the r ...

Caution: The React Hook useEffect is missing a required dependency

What is the best way to eliminate the warning "React Hook useEffect has a missing dependency" while developing my code? Here is a snippet of the code that triggers the warning: useEffect(() => { if(inactive){ document.querySelect ...

Deploying the app on Heroku and setting up the SSH key for secure

Having trouble pushing code to the Heroku server with this command $ heroku login Enter your Heroku credentials. Email: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b2b9abb3b7bab098b0b7acb5b9b1b4f6bbb7b5">[email pr ...

Setting a specific time zone as the default in Flatpickr, rather than relying on the system's time zone, can be

Flatpickr relies on the Date object internally, which defaults to using the local time of the computer. I am currently using Flatpickr version 4.6.6 Is there a method to specify a specific time zone for flatpickr? ...

What could be causing AngularJS to fail to send a POST request to my Express server?

I am currently running a Node Express server on localhost that serves a page with AngularJS code. Upon pressing a button on the page, an AngularJS controller is triggered to post a JSON back to the server. However, I am facing an issue where the post requ ...

Loading images in advance using JavaScript

I have been searching through multiple forums and cannot seem to figure out the issue. The problem I am encountering is related to a website where there are three images with a hover effect. When the page first loads, there is a delay in loading the backgr ...

leveraging an array from a separate JavaScript file within a Next.js page

I am facing a situation where I need to utilize an array from another page within my Next.js project. However, it seems that the information in the array takes time to load, resulting in encountering undefined initially when trying to access it for title a ...

What is the method for specifying a specific sub-dependency version in a package in Node.js?

Let me simplify my issue for you. I am facing troubles while trying to install two plugins, plugin A version 2.0 and plugin B version 3.0. It turns out that plugin B has plugin A as a sub-dependency with a conflicting version, resulting in a build phase e ...

Encountering issues while trying to deploy a Next JS 13 application on Google Cloud Platform's

Everything was functioning properly with Next version 12, however upon upgrading to Next 13 I encountered the following error. and current node version: "18.x.x" Next Js version: "13.2.1" Step #2: /app/node_modules/next/dist/build/index ...

In the discord.js library, the reactionCollector feature does not cease even when the stop() method is invoked

I'm currently developing a bot using discord.js and facing an issue. My objective is to create a reaction collector that collects the reactions of users who want to participate in a tournament when triggered by the command "tournament prepare". Later, ...

Passing an undefined value to the database via AJAX upon clicking a button

Hi there, I'm currently working on a table where I'm trying to perform an inline edit and update the value in the database by clicking on a button (an image). I've attempted to use an onclick function, but it seems to show "value=undefined&a ...

Issues encountered while loading the Tensorflow.js model in a bootstrap webpack demonstration

I've been working on loading a JSON-formatted model in Tensorflow.js using the Bootstrap webpack example from GitHub. Initially, when I used Tensorflow.js without bootstrap in webpack, the code worked perfectly either in webpack or if I included it v ...

Is there a way to retrieve data from the database prior to launching my Express generator application?

After creating an app with Express generator, you are provided with predefined code files to help run your app smoothly. However, I am struggling to figure out where exactly I should place the code to fetch data from the database before launching the app. ...

Navigate the Angular interceptor route to display a 404 error page when clicking on a `<a href="#">` tag

When using href="#" as a placeholder in html, I encountered an issue where Angular was unable to recognize it and would route to the 404 page despite having the following configuration in the module. How can this problem be resolved? .config( function m ...

Preventing default behavior in a JQuery post request

Encountering an issue with jQuery functionality on a mobile device. function send() { $.post("scripts/post.php", { username: $("input[name=username]").val(), password: $("input[name=password]").val() }, function(data) { if ($(".data div" ...

Dynamically insert <td> elements into <tr> element using both jQuery and JavaScript

I am facing an issue with adding a new table data (td) element dynamically to the first table row (tr) in my JavaScript code. Here is the original table before adding the new td element: <table> <tbody> <tr> <t ...

"I'm running into an issue with Heroku, Facebook, and NodeJS - it seems like the module 'OAuth' is

Having recently ventured into the world of NodeJS, I find myself facing what seems like a simple issue. Despite my best efforts, I have been unable to resolve it with my current understanding. To test some functionalities in a development environment on m ...

Utilizing AJAX and JavaScript to generate a table using the AJAX response and placing it within a <div> element

I am currently passing the response of this action in text form, but I would like to display it in a table format. Is there a way to do this? function loadAditivos(){ $('#aditivoAbertoInformacoesTexto').html('<div id="loaderMaior ...

Upon successful authorization, the Node Express server will pass the access token to the React client app via OAuth in the callback

I am currently working on a node server that authenticates with a third party using oauth, similar to how Stack Overflow does. After authorizing the request and obtaining the access token and other essential information from the third party, my goal is to ...

Display a loader while waiting for an API call to complete within 5 seconds using Angular and RxJS operators. If the API call takes longer

We are actively working to prevent user blockage during file uploads by implementing a method in Angular using RxJS. How can I display a toastr message and hide the loader if the API does not complete within 5 seconds? uploadFile() { this.service.uploa ...