How to get your Rest APi Response from Glitch Project
Well - well - well, Someone may want to get JSON or Some response from their REST APi Project. But, The Server didn’t response to the Response that client(You) want. Soo… How can we fix it?
You’re in right Post boi ( ͡° ͜ʖ ͡°)
Today, i will Tell you How to get your REST APi Response from your Glitch Project
Everything that you need:
NodeJS v8+,
request Module,
and Your REST API Glitch Project.
Okok, So what do i need to do Next?
Prepare your Client Project at your PC, Mobile, or at hosting Service (Example is Glitch.com)
In your Client Project, install request module. Yes, Only Client project.
Prepare your REST API server. Idle or Online is no problem.
In Client Project, Write This code below:
const request = require("request")
// Create request Variable.
// Now send request to your REST APi Server
// We will set User-Agent header since Glitch will throw you to Fake Waking Up page when you request it without User-Agent.
// Or it give 403 response code
// Set your User Agent here. Anything.
var myuseragent = "Get REST APi Response! - Your Client!";
request("https://rest-api.glitch.me", {
headers: {
"User-Agent": myuseragent
}
}, function (err, res, body) {
if (err) {
/* */
}
// Your Code
});
Run the client codes and See the Result.
Do not misuse this Code that can cause Glitch TOS Violation.
Alternative, You can try got module:
const got = require("got");
got("https://rest.api").then(r => {
let body = r.body;
//Your Code
});
Like what @khalby786 said, request has been deprecated in favor of newer, faster options like Fetch. Additionally, the fetch option doesn’t return any waking headers - there’s just a delay as the server wakes up and then your stuff appears.
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.
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!' }
});