Gavin
Gavin
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
Works like a charm, thank you! This is helping me get better at JS and JSON.
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
thank you again for all your help
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
all good, i might be a bit as well
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
oh sorry no I am getting:
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
I am sorry but I need a little more help xD, I am struggling to incorporate the code into my project so here is my project thus far with my attempt of incorporating them
export default {
async fetch(req) {
const url = new URL(req.url);
var firstnames = ['this', 'was', 'a', 'long', 'list'];
var lastnames = ['this', 'was', 'a', 'long', 'list'];
var colors = ['this', 'was', 'a', 'long', 'list'];
var cities = ['this', 'was', 'a', 'long', 'list'];

const type = url.searchParams.get('type');
var apiresponse="Error: 404"
if (type === null) {var apiresponse="An error has occurred."}

if (type === 'name') {var firstnamerandom= Math.floor((Math.random() * firstnames.length)); var lastnamerandom= Math.floor((Math.random() * lastnames.length)); var apiresponse=`{ "firstname": "` + firstnames[firstnamerandom] + `", "lastname": "` + lastnames[lastnamerandom] + `" }`;}

if (type === 'color') {var colorrandom= Math.floor((Math.random() * colors.length)); var apiresponse=`{ "hex": "` + colors[colorrandom] + `" }` }

if (type === 'city') {var cityrandom= Math.floor((Math.random() * cities.length)); var apiresponse=`{ "city": "` + cities[cityrandom] + `" }` }

if (type === 'colorname') {var apiresponse=handleRequest(request)}


return new Response(apiresponse);
}
}



async function gatherResponse(response) {
const { headers } = response;
const contentType = headers.get("content-type") || "";
return response.json();
}

async function handleRequest(request) {
const { searchParams } = new URL(request.url);
let type = searchParams.get("type");
let hex = searchParams.get("hex");
let url = "https://www.thecolorapi.com/id?hex=" + hex;
const init = {
headers: { "content-type": "text/plain" },
};
const response = await fetch(url, init);
const results = await gatherResponse(response);
const colorName = JSON.stringify(results.name.value);
return new Response(colorName, init);
}


export default {
async fetch(req) {
const url = new URL(req.url);
var firstnames = ['this', 'was', 'a', 'long', 'list'];
var lastnames = ['this', 'was', 'a', 'long', 'list'];
var colors = ['this', 'was', 'a', 'long', 'list'];
var cities = ['this', 'was', 'a', 'long', 'list'];

const type = url.searchParams.get('type');
var apiresponse="Error: 404"
if (type === null) {var apiresponse="An error has occurred."}

if (type === 'name') {var firstnamerandom= Math.floor((Math.random() * firstnames.length)); var lastnamerandom= Math.floor((Math.random() * lastnames.length)); var apiresponse=`{ "firstname": "` + firstnames[firstnamerandom] + `", "lastname": "` + lastnames[lastnamerandom] + `" }`;}

if (type === 'color') {var colorrandom= Math.floor((Math.random() * colors.length)); var apiresponse=`{ "hex": "` + colors[colorrandom] + `" }` }

if (type === 'city') {var cityrandom= Math.floor((Math.random() * cities.length)); var apiresponse=`{ "city": "` + cities[cityrandom] + `" }` }

if (type === 'colorname') {var apiresponse=handleRequest(request)}


return new Response(apiresponse);
}
}



async function gatherResponse(response) {
const { headers } = response;
const contentType = headers.get("content-type") || "";
return response.json();
}

async function handleRequest(request) {
const { searchParams } = new URL(request.url);
let type = searchParams.get("type");
let hex = searchParams.get("hex");
let url = "https://www.thecolorapi.com/id?hex=" + hex;
const init = {
headers: { "content-type": "text/plain" },
};
const response = await fetch(url, init);
const results = await gatherResponse(response);
const colorName = JSON.stringify(results.name.value);
return new Response(colorName, init);
}


52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
Perfect! Thank you for all your help. I will try incorporating this now.
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
kk
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
Also I am sure you know but just to make sure the URL would be more like https://worker.url/?type=color&hex=FFFFFF
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
Yes kinda, I want to add other data as well
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
Yeah my syntax is crazy, i have other if statements with it to act as pages on the api
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
So here is my code that I have adopted into my base project:
export default {
async fetch(req) {
const url = new URL(req.url);
const type = url.searchParams.get('type');
var apiresponse="Error: 404"

if (type === 'colorname') {const hex = url.searchParams.get('hex'); const url = "https://www.thecolorapi.com/id?hex=" + hex; const init = {headers: { "content-type": "text/plain" },}; const response = await fetch(url, init); const results = await gatherResponse(response); const apiresponse = JSON.stringify(results.name.value)}


return new Response(apiresponse);
}
}

async function gatherResponse(response) {
const { headers } = response;
const contentType = headers.get("content-type") || "";
return response.json();
}


export default {
async fetch(req) {
const url = new URL(req.url);
const type = url.searchParams.get('type');
var apiresponse="Error: 404"

if (type === 'colorname') {const hex = url.searchParams.get('hex'); const url = "https://www.thecolorapi.com/id?hex=" + hex; const init = {headers: { "content-type": "text/plain" },}; const response = await fetch(url, init); const results = await gatherResponse(response); const apiresponse = JSON.stringify(results.name.value)}


return new Response(apiresponse);
}
}

async function gatherResponse(response) {
const { headers } = response;
const contentType = headers.get("content-type") || "";
return response.json();
}


And it is throwing an 500 Internal Server Error
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
Coolio, I will try it out now. Thanks!
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
kk
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
ok i will try to use it now, one sec
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
ohhhh i see, wow that is pretty straight forward
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
{"hex":{"value":"#FFFFFF","clean":"FFFFFF"},"rgb":{"fraction":{"r":1,"g":1,"b":1},"r":255,"g":255,"b":255,"value":"rgb(255, 255, 255)"},"hsl":{"fraction":{"h":0,"s":0,"l":1},"h":0,"s":0,"l":100,"value":"hsl(0, 0%, 100%)"},"hsv":{"fraction":{"h":0,"s":0,"v":1},"value":"hsv(0, 0%, 100%)","h":0,"s":0,"v":100},"name":{"value":"White","closest_named_hex":"#FFFFFF","exact_match_name":true,"distance":0},"cmyk":{"fraction":{"c":0,"m":0,"y":0,"k":0},"value":"cmyk(0, 0, 0, 0)","c":0,"m":0,"y":0,"k":0},"XYZ":{"fraction":{"X":0.95047,"Y":1,"Z":1.08883},"value":"XYZ(95, 100, 108.883)","X":95,"Y":100,"Z":109},"image":{"bare":"https://www.thecolorapi.com/id?format=svg&named=false&hex=FFFFFF","named":"https://www.thecolorapi.com/id?format=svg&hex=FFFFFF"},"contrast":{"value":"#000000"},"_links":{"self":{"href":"/id?hex=FFFFFF"}},"_embedded":{}}
{"hex":{"value":"#FFFFFF","clean":"FFFFFF"},"rgb":{"fraction":{"r":1,"g":1,"b":1},"r":255,"g":255,"b":255,"value":"rgb(255, 255, 255)"},"hsl":{"fraction":{"h":0,"s":0,"l":1},"h":0,"s":0,"l":100,"value":"hsl(0, 0%, 100%)"},"hsv":{"fraction":{"h":0,"s":0,"v":1},"value":"hsv(0, 0%, 100%)","h":0,"s":0,"v":100},"name":{"value":"White","closest_named_hex":"#FFFFFF","exact_match_name":true,"distance":0},"cmyk":{"fraction":{"c":0,"m":0,"y":0,"k":0},"value":"cmyk(0, 0, 0, 0)","c":0,"m":0,"y":0,"k":0},"XYZ":{"fraction":{"X":0.95047,"Y":1,"Z":1.08883},"value":"XYZ(95, 100, 108.883)","X":95,"Y":100,"Z":109},"image":{"bare":"https://www.thecolorapi.com/id?format=svg&named=false&hex=FFFFFF","named":"https://www.thecolorapi.com/id?format=svg&hex=FFFFFF"},"contrast":{"value":"#000000"},"_links":{"self":{"href":"/id?hex=FFFFFF"}},"_embedded":{}}
I want: "value":"White"
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
Yes, I have seen it but how to a get a specific piece of the json and not the whole thing?
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
Awesome, thanks!
52 replies
CDCloudflare Developers
Created by Gavin on 1/9/2023 in #workers-help
JSON to Variable
Oh I see, yes I am building this in a cloudflare worker.
52 replies