API response in the browser different from the one in the code - javascript

Here's my code:
async function BuiltWithCall(website) {
var domainCall = `https://api.builtwith.com/v12/api.json?KEY=${keys.builtWith}&LOOKUP=${website}`;
var domainRes = await fetch(domainCall);
console.log(domainRes);
var keywordCall = `https://api.builtwith.com/kw1/api.json?KEY=${keys.builtWith}&LOOKUP=${website}`;
var keywordRes = await fetch(keywordCall);
console.log(keywordRes);
return await {'domRes': domainRes.json(), 'kwRes': keywordRes.json()};
}
It takes the website provided and runs it through the BuiltWith API. But the problem is the response.
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]:
{ body:
PassThrough {
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_eventsCount: 7,
_maxListeners: undefined,
_writableState: [Object],
writable: true,
allowHalfOpen: true,
_transformState: [Object] },
disturbed: false,
error: null },
[Symbol(Response internals)]:
{ url: 'https://api.builtwith.com/v12/api.json?KEY=key&LOOKUP=hotelscombined.com',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object] } } }
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]:
{ body:
PassThrough {
_readableState: [Object],
readable: true,
domain: null,
_events: [Object],
_eventsCount: 3,
_maxListeners: undefined,
_writableState: [Object],
writable: false,
allowHalfOpen: true,
_transformState: [Object] },
disturbed: false,
error: null },
[Symbol(Response internals)]:
{ url: 'https://api.builtwith.com/kw1/api.json?KEY=key&LOOKUP=hotelscombined.com',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object] } } }
So this right here makes no sense to me. Because this is the exact same URL, run in a browser:
{
"Keywords": [
{
"Domain": "hotelscombined.com",
"Keywords": [
"compare",
"save",
"cheap",
"hotel",
"deal",
"hotelscombined",
"search",
"... More keywords but you get the idea"
]
}
],
"Errors": []
};
Other call response:
{
"Results": [
{
"Result": {
"IsDB": true,
"Spend": 609,
"Paths": [
{
"FirstIndexed": 1294059600000,
"LastIndexed": 1526338800000,
"Domain": "builtwith.com",
"Url": "",
"SubDomain": "",
"Technologies": [
{
"Categories": [
"Edge Delivery Network"
],
"IsPremium": "yes",
"Name": "Amazon CloudFront",
"Description": "Amazon CloudFront delivers your static and streaming content using a global network of edge locations.",
"Link": "http://aws.amazon.com/cloudfront/",
"Tag": "cdns",
"FirstDetected": 1386284400000,
"LastDetected": 1526338800000
},
]
},
]
},
"Meta": {
"Vertical": "Technology And Computing",
"Social": [
"http://twitter.com/builtwith",
"http://facebook.com/builtwith",
"http://linkedin.com/company/builtwith",
"http://google.com/+builtwithdotcom"
],
"CompanyName": "BuiltWith",
"Telephones": [
"+61-300-558745",
"+1-650-618-3949"
],
"Emails": [
"support#builtwith.com"
],
"City": "Sydney",
"State": "NSW",
"Postcode": "2000",
"Country": "AU",
"Names": [
{
"Name": "N/A",
"Type": 0,
"Email": "n/a#builtwith.com"
},
{
"Name": "N/A",
"Type": 0,
"Email": "n/a#builtwith.com"
}
],
"ARank": 22108,
"QRank": 275921
},
"Attributes": {
"MJRank": 8737,
"MJTLDRank": 4620,
"RefSN": 7402,
"RefIP": 10142,
"TTFB": 129,
"Sitemap": 20,
"GTMTags": 0,
"QubitTags": 0,
"TealiumTags": 0,
"AdobeTags": 0,
"CDimensions": 0,
"CGoals": 0,
"CMetrics": 0,
"SourceBytes": 0
},
"FirstIndexed": 1294059600000,
"LastIndexed": 1526338800000,
"Lookup": "builtwith.com"
}
],
"Errors": []
}
So as you can see the responses are completely different, and I have no idea why. The same fetch method works perfectly for PageSpeed API, but here something is going horribly wrong.
PageSpeed call:
async function PageSpeedCall(website) {
var pagespeedCall = `https://www.googleapis.com/pagespeedonline/v4/runPagespeed?url=https://${website}&strategy=mobile&key=${keys.pageSpeed}`;
// second call
var results = await fetch(pagespeedCall);
return await results.json();
}
What am I doing wrong?

domainRes is a Response object, not a payload. That is why you see all the stuff in the console output.
To parse the payload as JSON you need to call domainRes.json which also gives you a promise so you have to await for it. Like this.
async function BuiltWithCall(website) {
var domainCall = `https://api.builtwith.com/v12/api.json?KEY=${keys.builtWith}&LOOKUP=${website}`;
var domainRes = await fetch(domainCall);
console.log(domainRes);
var keywordCall = `https://api.builtwith.com/kw1/api.json?KEY=${keys.builtWith}&LOOKUP=${website}`;
var keywordRes = await fetch(keywordCall);
console.log(keywordRes);
return {'domRes': await domainRes.json(), 'kwRes': await keywordRes.json()};
}

Related

Fetching information from Roblox API about a game

I am trying to fetch information from: https://games.roblox.com/v1/games?universeIds=3759152694
But having it try to response with "response.data[0]" (I use jsonpathfinder) it gives me undefined. When I try to do "response.data[0].id" it says that id is not defined. I have not messed with apis in quite a while so I may be just missing something here. Any help is appreciated!
const axios = require("axios");
module.exports = {
name: "info",
execute: async (bot, message, args) => {
test = {
data: [
{
id: 3759152694,
rootPlaceId: 10269801695,
name: "unnamed sim",
description: "soon",
sourceName: "unnamed sim",
sourceDescription: "soon",
creator: { id: 3593901883, name: "Jack1286401", type: "User", isRNVAccount: false, hasVerifiedBadge: false },
price: null,
allowedGearGenres: ["All"],
allowedGearCategories: [],
isGenreEnforced: false,
copyingAllowed: false,
playing: 0,
visits: 0,
maxPlayers: 6,
created: "2022-07-18T14:21:46.03Z",
updated: "2022-07-22T08:17:08.8213451Z",
studioAccessToApisAllowed: false,
createVipServersAllowed: false,
universeAvatarType: "MorphToR15",
genre: "All",
isAllGenre: true,
isFavoritedByUser: false,
favoritedCount: 0,
},
],
};
await axios.get("https://games.roblox.com/v1/games?universeIds=3759152694").then(function (response) {
// handle success
console.log(JSON.stringify(response.data[0]));
});
},
};
JSON Table:
{
"data": [
{
"id": 3759152694,
"rootPlaceId": 10269801695,
"name": "unnamed sim",
"description": "soon",
"sourceName": "unnamed sim",
"sourceDescription": "soon",
"creator": { "id": 3593901883, "name": "Jack1286401", "type": "User", "isRNVAccount": false, "hasVerifiedBadge": false },
"price": null,
"allowedGearGenres": ["All"],
"allowedGearCategories": [],
"isGenreEnforced": false,
"copyingAllowed": false,
"playing": 0,
"visits": 0,
"maxPlayers": 6,
"created": "2022-07-18T14:21:46.03Z",
"updated": "2022-07-22T08:17:08.8213451Z",
"studioAccessToApisAllowed": false,
"createVipServersAllowed": false,
"universeAvatarType": "MorphToR15",
"genre": "All",
"isAllGenre": true,
"isFavoritedByUser": false,
"favoritedCount": 0
}
]
}

How to properly create objects instead of arrays in Javascript

I am trying to create a object using my placeholder. The current code works however it is creating array in my Children property. How can I recode so that my output is object
const placeholder = (index, isRootDirectory) => {
const create = {
id: index,
title: null,
location: index,
isRootDirectory,
padStyle: [
{
icon: null,
color: null,
},
],
UUID: null,
action: null,
Children: [],
};
return create;
};
const createLayout = () => {
let root = {
info: "Custom Layout",
Children: [],
};
for (let i = 0; i < 2; i++) {
root.Children.push(placeholder(i, true));
console.log(root);
}
console.log(root);
};
Current output:
{"Children": [{"Children": [Array], "UUID": null, "action": null, "id": 0, "isRootDirectory": true, "location": 0, "padStyle": [Array], "title": null}, {"Children": [Array], "UUID": null, "action": null, "id": 1, "isRootDirectory": true, "location": 1, "padStyle": [Array], "title": null}], "info": "Custom Layout"}
Desired output
{"Children": [{"Children": [object], "UUID": null, "action": null, "id": 0, "isRootDirectory": true, "location": 0, "padStyle": [object], "title": null}, {"Children": [object], "UUID": null, "action": null, "id": 1, "isRootDirectory": true, "location": 1, "padStyle": [object], "title": null}], "info": "Custom Layout"}
In your code where you defined the constant create, you defined the Children key as an array by writing Children: []. Instead, write Children: {}.
So
const create = {
id: index,
title: null,
location: index,
isRootDirectory,
padStyle: [
{
icon: null,
color: null,
},
],
UUID: null,
action: null,
Children: [],
};
Would be written as
const create = {
id: index,
title: null,
location: index,
isRootDirectory,
padStyle: [
{
icon: null,
color: null,
},
],
UUID: null,
action: null,
Children: {},
};

How to transform api response in nestjs?

I am using Nest Serialization to transform api response. However, it returns really crypic result. I expect it to slice some fields out of the data but for some reason, it shows weird result.
product.entity.js
export class ProductEntity {
id: string
description: string
status: boolean
price: string
moq: number
rating: number
last_modified: string
constructor(partial: Partial<ProductEntity>) {
Object.assign(this, partial)
}
}
product.controller.ts
#UseInterceptors(ClassSerializerInterceptor)
#Get()
async findAll(
#Query('page') page: number,
#Query('per_page') per_page: number
): Promise<ProductEntity[]> {
if (!page && !per_page) {
throw new BadRequestException('Proper query params not supplied')
}
const data: ProductEntity[] = []
const products: any = await this.productService.findAll({
page,
per_page
})
products.map(product => {
data.push(new ProductEntity(product))
})
return data
}
product.service.ts
async findAll(query): Promise<Product[]> {
const { page, per_page }: { page: number; per_page: number } = query
const from: number = (page - 1) * per_page
return this.productModel
.find()
.skip(from)
.limit(Number(per_page))
}
which returns,
"$__": {
"strictMode": true,
"selected": {},
"getters": {},
"_id": {
"_bsontype": "ObjectID",
"id": {
"type": "Buffer",
"data": [
93,
15,
149,
140,
78,
148,
77,
10,
10,
105,
51,
130
]
}
},
"wasPopulated": false,
"activePaths": {
"paths": {
"moq_type": "init",
"moq": "init",
"price_type": "init",
"price": "init",
"name": "init",
"category_id": "init",
"company_id": "init",
"short_description": "init",
"long_description": "init",
"active": "init",
"variations": "init",
"keywords": "init",
"photos": "init",
"_id": "init",
"created_at": "init",
"updated_at": "init",
"__v": "init"
},
"states": {
"ignore": {},
"default": {},
"init": {
"_id": true,
"short_description": true,
"long_description": true,
"active": true,
"variations": true,
"keywords": true,
"photos": true,
"name": true,
"price": true,
"price_type": true,
"moq": true,
"moq_type": true,
"category_id": true,
"company_id": true,
"created_at": true,
"updated_at": true,
"__v": true
},
"modify": {},
"require": {}
},
"stateNames": [
"require",
"modify",
"init",
"default",
"ignore"
]
},
"pathsToScopes": {},
"cachedRequired": {},
"session": null,
"$setCalled": [],
"emitter": {
"_events": {},
"_eventsCount": 0,
"_maxListeners": 0
},
"$options": {
"skipId": true,
"isNew": false,
"willInit": true
}
},
Am I doing something wrong in here or is it not correct way to transform API response?
Not sure about NestJS but in regular node.js for mongodb, If you are calling find() that way X().X().X() you need to add at the end of the pipeline the call to exec() method, like :
this.productModel
.find()
.skip(from)
.limit(Number(per_page))
.exec();
I am getting that too. The answer is removed #UseInterceptors(ClassSerializerInterceptor) in your controller.
Maybe that function utilize object inheritance from class extension? I think you don't really need it because that function is used to cast a response (like tranforming camerCase to snake_case, etc). If you really need it, then I don't know because I am still strugle with it too.

New to node.js here. How do I properly get the JSON from the top of a subreddit?

I am trying to call a get on this api url here: http://www.reddit.com/r/reddevils/top.json?sort=top&t=day&limit=1
and all I am trying to do is grab the permalink data, which is an element inside of an object called 'data' which is nested in another object called 'data'. I need the permalink data, so, I can export it and use it in another file.
I am trying to do this on node.js, how would I go about doing this? I have attempted to use the http, express, and ajax-request packages. I feel like I am on the right path, but it may be just a matter of proper syntax.
This is what I have right now. I was just trying to print out the JSON object onto my console to see if I am on the right path:
var request = require('ajax-request');
var http = require('http');
var url = "http://www.reddit.com/r/reddevils/top.json?sort=top&t=day&limit=1";
http.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var redditResponse = JSON.parse(body);
console.log("Got a response: ", redditResponse.picture);
});
}).on('error', function(e){
console.log("Got an error: ", e);
});
Thanks
When formatted, your data looks like this:
{
"kind": "Listing",
"data": {
"modhash": "fop7jgfq8z07efe8b23281496fdcd30eae5038b50d6b9811d2",
"children": [{
"kind": "t3",
"data": {
"contest_mode": false,
"subreddit_name_prefixed": "r/reddevils",
"banned_by": null,
"media_embed": {},
"thumbnail_width": 140,
"subreddit": "reddevils",
"selftext_html": null,
"selftext": "",
"likes": null,
"suggested_sort": null,
"user_reports": [],
"secure_media": null,
"link_flair_text": "TIER 0",
"id": "6jqxwx",
"view_count": null,
"secure_media_embed": {},
"clicked": false,
"report_reasons": null,
"author": "SuperShadowJr",
"saved": false,
"mod_reports": [],
"name": "t3_6jqxwx",
"score": 792,
"approved_by": null,
"over_18": false,
"domain": "instagram.com",
"hidden": false,
"preview": {
"images": [{
"source": {
"url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?s=5fe73832273b3e88324a6da17fa55fae",
"width": 640,
"height": 640
},
"resolutions": [{
"url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=108&s=b668298ccd3a69304ab23c5aef1b6d77",
"width": 108,
"height": 108
}, {
"url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=216&s=706a70a275e20973e8ce772f362db87f",
"width": 216,
"height": 216
}, {
"url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=320&s=42c33880cf1a0a1c14e814de5469a1a1",
"width": 320,
"height": 320
}, {
"url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=640&s=783b2011d7523d9c84450293eef9d6c0",
"width": 640,
"height": 640
}],
"variants": {},
"id": "TNO0lZhaqwUX-vbTYG6y0bI6hZhhgQJWTgm_OVk7b2E"
}],
"enabled": false
},
"thumbnail": "https://a.thumbs.redditmedia.com/8U2J6sELZ1AIGDsReUz-v10X3qyFG49TBH0XrjXAMc8.jpg",
"subreddit_id": "t5_2rxse",
"edited": false,
"link_flair_css_class": "black",
"author_flair_css_class": null,
"gilded": 0,
"downs": 0,
"brand_safe": true,
"archived": false,
"removal_reason": null,
"post_hint": "link",
"can_gild": true,
"thumbnail_height": 140,
"hide_score": false,
"spoiler": false,
"permalink": "/r/reddevils/comments/6jqxwx/fabinhos_girlfriendwife_likes_ig_picture_of/",
"num_reports": null,
"locked": false,
"stickied": false,
"created": 1498574067.0,
"url": "https://www.instagram.com/p/BV0dLySgy82/",
"author_flair_text": null,
"quarantine": false,
"title": "Fabinho's girlfriend/wife likes IG picture of Fabinho in MUFC kit with the caption \"Could we see this man in the PL?\"",
"created_utc": 1498545267.0,
"distinguished": null,
"media": null,
"num_comments": 79,
"is_self": false,
"visited": false,
"subreddit_type": "public",
"is_video": false,
"ups": 792
}
}],
"after": "t3_6jqxwx",
"before": null
}
}
So, you can use:
redditResponse.data.children[0].data.permalink
Working demo: https://jsfiddle.net/jfriend00/nkpchrk0/
If you want all the children, then you need to iterate i in:
redditResponse.data.children[i].data.permalink
to fetch each one.
Or, to get them all at once into their own array, you could do this:
let permaLinks = redditResponse.data.children.map(function(child) {
return child.data.permalink;
});

Accessing [Object] from POST response

How do I access the [Object] and return the value for each object to the console?
Response:
[ 'qpxExpress#tripsSearch',
{ kind: 'qpxexpress#tripOptions',
requestId: 'EWwQPoK0fma2Wgz1x0PNGc',
data:
{ kind: 'qpxexpress#data',
airport: [Object],
city: [Object],
aircraft: [Object],
tax: [Object],
carrier: [Object] },
tripOption: [ [Object] ] } ]
Request:
const options = {
method: 'POST',
uri: 'https://www.googleapis.com/qpxExpress/v1/trips/search?&key=XXXXXXXXXXXXXXXXX',
body: {
"request": {
"passengers": {
"adultCount": "1"
},
"slice": [
{
"origin": "SFO",
"destination": "LAX",
"date": "2017-09-19"
}
],
"solutions": "1"
}
},
json: true
}
Just needed to call on the nested object.
Reading the api documentation helped.
Updated code that returns the total price:
request(options)
.then(function (response) {
console.log(response.trips.tripOption[0].saleTotal)
})
.catch(function (err) {
console.log(err)
})

Categories

Resources