The website TryHackMe came up with a challenge that involves using HTTP requests to interface with REST APIs (here, task #14/day 9). The challenge basically involves querying a basic REST API to reconstruct a string value from the information returned by the service. Specifically, the value
key has a piece of the string to be appended to a final string, and the next
key has a subdirectory to visit for the next value
to be appended to the final string. The solution is supposed to repeat this procedure until the value
key has a value of "end".
I published a writeup that discusses this further (if the paywall is giving you problems, just open this link in private browsing ;-):
- "Aleksey" (2023). Hacking REST APIs with JavaScript. JavaScript In Plain English. Link: https://javascript.plainenglish.io/hacking-rest-apis-with-javascript-ecf39e38c21f
Here are some things that I am curious about:
- What are your initial & overall impressions of the code?
- Is there a better way of writing an implementation?
- A bit of a rehash of the second one, but what do you not like about my code (assuming that you notice flaws)?
- A bit of a meta question: do you think I can present my problem & solution a little better next time?
And of course, my JavaScript solution
/*
* An implementation to the Advent of Cyber's "Requests" task.
* (Partially) ported from Hamdan (2021)'s solution
* Implemented by Aleksey
* - GitHub: https://github.com/Alekseyyy
* - Keybase: https://keybase.io/epsiloncalculus
*/
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const xhr = new XMLHttpRequest();
const url = "http://requests.thm:3000/";
let currentPath = "";
let flag = "";
let done = false;
while (!done) {
if (currentPath.toLowerCase() === "end") {
done = true;
break;
}
xhr.open("GET", url + currentPath, false);
xhr.send();
const myJson = JSON.parse(xhr.responseText);
flag += myJson["value"];
currentPath = myJson["next"];
}
console.log(flag);
/* References
* Hamdan, M. (2021). Python For Web Automation | TryHackMe
* Advent Of Cyber 1 Day 9. YouTube Video. Retrieved on Feb.
* 12, 2023 from: https://youtu.be/zFeLExZNPso
*/