That makes me wonder, is this tutorial for client or server side? If it’s server side you can use axios or postman-fetch or something to get an API.
Or if you want to be a total power user (and waste lots of time) curl
the api endpoint, write it to a file with echo >>
(I think) and then read the file with javascript.
In Node, you can use node-fetch
, which is Fetch API for Node.js.
const fetch = require('node-fetch');
fetch("https://example.com")
.then(res => res.text())
.then(data => console.log(data))
Is it built in? Are there any built in fetch modules?
No, you need to first install node-fetch.
# NPM
npm i node-fetch --save
# YARN
yarn add node-fetch
const fetch = require('node-fetch');
fetch("https://example.com")
.then(res => res.text())
.then(data => console.log(data))
why not res.json() ?
Also what’s the benefit of yarn/pnpm over normal npm?
I used res.text()
for the sake of the example
It’s faster. Remember, time is precious
It would be nice if Glitch had yarn, its faster from what I’ve heard.
Yeah, I think you can install yarn on Glitch using npm. And don’t use the Add Package button for this.
npm i yarn
You’re actually supposed to install yarn globally but you can’t do global installations in Glitch.
I suppose you could just add the “g” switch.
Global? Like -g
?
Yep, maybe. Not the NPM expert sorry, hehe
That’s what I said, you can’t install npm packages globally on Glitch.
Oh that sucks.
Some trick: Install it with --save-dev
Instead.
My newest question post may be resolved using this.
Did you mean I need "User-Agent":"Get REST APi Response! - Your Client!"
in my headers field? - I’m not sure if I have this ‘User-Agent’ field in ky
.
I am using Nuxt.js/$http - which is using the new ky
module.
Looks like ky
doesn’t have an option for setting request headers, although some GitHub issues that I’ve come across suggests using something like this:
ky.extend({
headers: { 'User-Agent': 'Get REST APi Response! - Your Client!' }
});
ah I know how to set headers via plugin in Nuxt.js.
That string looked random and I wasn’t sure.
I’ll try this and see if it gets fixed.
Thanks
Edit: fixed. Either this or hardcoding the GET/POST method URL with the full server URL prefix solved it. Thanks
Just updated the post.
Updated what?
A post.
Just see at the Top.