#npm request deprecated

76 messages · Page 1 of 1 (latest)

lusty crane

The npm request (https://www.npmjs.com/package/request) is now deprecated but I'm not sure what else to use to replace it. To give an idea of what I'm using it for; I wrote a code that automatically post new posts from a subreddit to a specific channel in the server (it was written on a older version of discord.js and I'm revisiting it.)

winged bough

Use fetch

lusty crane

How do you suggest I use it?

Wait the opld fetch or node-fetch

I'm unsure of which

winged bough

Fetch is built in on nodejs version 18+ I believe, if you have an older version then sure you can use node-fetch. It's like, the same thing as request tbh

fetch(url, options);
//options is an object as follows:
options = {
   method: //GET, PUT, PATCH, etc
   headers: //Object of headers
   body: //Body of request
};

/*
  fetch(url) is the same as
  fetch(url, { method: "GET" })
*/
lusty crane

Okay, got ya do i need to uninstall the old fetch i just installed?

winged bough

I don't think it would mess with the built in fetch, you can though because like, you're not gonna use it

Ofc, you can check the nodejs website about fetch

lusty crane

How do i uninstall npm lol, sorry...I havent coded in a while

winged bough

npm uninstall <package> I believe should remove the package

winged bough
lusty crane

Do you think axios is any better

I'm just a little confused on how to use fetch to do what request was doing honestly (im reading the docs, dont worry)

winged bough

I don't have much experience with axios but I would think a built in function is better than a 3rd party

And I'm not sure why i keep saying nodejs, fetch is a js thing

lusty crane

Got ya

winged bough

Lemme get the docs on it for you

winged bough

reddit would be a Response class

structure would be the json object the url returns

opal dawnBOT

mdn Fetch API
The Fetch API provides an interface for fetching resources (including across the network). It is a more powerful and flexible replacement for XMLHttpRequest.

winged bough

And log the return values

lusty crane

Okay, one moment.

Also thanks os much for helping me out

What would I console.log()?

The structure?

winged bough

Whatever you'd like to see, if you want to see watch fetch returns console log reddit. If you'd like to see what the json method returns console log structure

The json method returns the json version of the body, if that makes sense

lusty crane

Dammit, i might give up on this. im a little confused now

winged bough

You're just logging whatever you want to see

Since you're not sure on how it works, test it out and see what each function returns and stuff

lusty crane
winged bough

Or at the top level body of modules but yeah, jus put it in an async function then,

(async() => {
//code
})();

And run the test file

lusty crane

oksy hang tght

Do I make subreddit a variable?

winged bough

Yeah, you can make the code however you want I'm just allowing you to view what the functions return

fetch(url) returns a promise, that promise resolves the Response class. fetch(url) is a GET request which is just like request.get()

response.json() transforms the body of the response to a json object

It also returns a promise

lusty crane

Like this right>

winged bough

Well, Response isn't defined

Change that to reddit

lusty crane

oop

winged bough

reddit is your Response class, cuz that's what fetch returns and you stored that into the reddit variable

lusty crane

OHHHHH

I was lost on that but now I got you

winged bough

You're logging the response class, when you run the file you should see a bigass object log into your console. That's the response class

To log the json, log structure

lusty crane

Not sure what this means

winged bough

Whatever link you're fetching does not return a json

But rather an html

lusty crane

OH

Nvm i know how to fix that

one sec

winged bough
lusty crane

i can add /new.json to get the json

Okay, idk what any of these values are

but im getting somwhere

winged bough

Did you log structure?

If you did, then that's what the link you're fetching returns, that's the json

lusty crane

yeah

winged bough

Click the link and you can view it on your browser yourself to see

I can't help you further with anything on that site because I dont know what it contains, but you just learnt how to make a GET request using fetch and how to transform the response's body to a json

If the body of the response is not a json, you will get that error youve gotten just now

That's basically it, you've done exactly what you did using request but with fetch

lusty crane

What if I did something like let posts = structure.data.children;

winged bough

Then posts would be the array of objects you see under the data.children property in the log

structure is a normal object

Idk why I kept saying convert to a json, it converts the json in the response body to a normal object. That's basically it

lusty crane

Imms head to bed but need more helping, ill update