0

I'm currently trying to access the JokeAPI using Node's module HTTPS, where below is my code:

const https = require('https');
let url = 'https://v2.jokeapi.dev/joke/Any';
https.get(url, function (res) {
    console.log(res);
})

However, somehow I keep getting this error:

node:events:498
      throw er; // Unhandled 'error' event
      ^

Error: self signed certificate in certificate chain
    at TLSSocket.onConnectSecure (node:_tls_wrap:1530:34)
    at TLSSocket.emit (node:events:520:28)
    at TLSSocket._finishInit (node:_tls_wrap:944:8)
    at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:725:12)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketErrorListener (node:_http_client:442:9)
    at TLSSocket.emit (node:events:520:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'SELF_SIGNED_CERT_IN_CHAIN'

Is it because I don't have a server set up? I'm a beginner with JS and Node, so any help would be very appreciated. Thank you!

Edit: I actually added 'process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';' to the top of the code and it worked, but now I'm getting an 'undefined' for a response body. And I'm not sure why that is happening :(

  • As far as I can tell, the API's certificate is valid. What OS and what version of Node are you using? – Phil Mar 08 '22 at 00:37
  • Does this answer your question? [nodejs - error self signed certificate in certificate chain](https://stackoverflow.com/questions/45088006/nodejs-error-self-signed-certificate-in-certificate-chain) – Phil Mar 08 '22 at 00:40
  • I'm on a Windows, using Node v16.14.0 – wannabeengineer Mar 08 '22 at 00:41
  • Ah thanks Phil! Adding the process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; to the javascript file worked, not too sure why this error happens even after reading through that post or what the whole thing is about.. lol – wannabeengineer Mar 08 '22 at 00:45
  • However, now I'm getting an 'undefined' for a response body. I'm not too sure what's causing this. – wannabeengineer Mar 08 '22 at 00:54
  • See the [example in the documentation](https://nodejs.org/api/https.html#httpsgeturl-options-callback). You need to read the response stream – Phil Mar 08 '22 at 00:56

1 Answers1

0

My system does not show the self signed certificate in certificate chain error, but it sounds like you have a work-around for that.

However, now I'm getting an 'undefined' for a response body. I'm not too sure what's causing this.

https.get() just sends the request and reads the headers. It does not, by itself, read the whole response. To do that, you need to listen for the data event on the res object. Installing the listener for the data event will cause the stream to start reading data from the incoming request. This data will not necessarily come all at once. It can come broken into multiple data events so you would really need to accumulate all the data and then in the end event, you know you have all the data. You can see a full example for how to implement that here in the http.get() doc.

Here's a simpler version that will just show you what's in each data event.

const https = require('https');
let url = 'https://v2.jokeapi.dev/joke/Any';
https.get(url, function(res) {
    res.on('data', data => {
        console.log(data.toString());
    });
});

FYI, I no longer use plain https.get() because all the higher level libraries listed here are more convenient to use and all support promises which is generally an easier way to program with asynchronous requests.

My favorite library from that list is got(), but you can choose whichever one you like the API and/or features for. Here's an implementation using got():

const got = require('got');
const url = 'https://v2.jokeapi.dev/joke/Any';

got(url).json().then(result => {
    console.log(result);
}).catch(err => {
    console.log(err);
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979