Making an HTTP GET request without any outside dependencies

Hello everyone, I come to you in dire need. I am taking a code test for a job and they have given me a very strange question.
I need to request information from a database, however I cannot add anything to the testing environment.
It tells me to get HTTP GET however both HTTP and GET throw errors.
I cannot add fetch, new XMLhttprequest also is undefined.
I’m not asking for help to solve the wholeproblem, I am just stuck on this roadblock.
How do I request info from an api without any of the techniques that I’ve even heard of?
What do they want me to use?
Please help, I have been at it for 2 hours and gotten absolutely nowhere.
This is all the code that was given to me.

async function getUserTransaction(uid, txnType, monthYear) {
}

Is it frontend or backend? What does the description say, exactly?

Thanks for your reply. It won’t let me copy and paste so here is an image.

1

What’s the previous question? And does this one follow on from it?

No, this is the first question. There’s another part to it but I am stuck just requesting information from the api given.

What does your code looks like?
Do you have any tech/environment constrains?

I am curious why you said you cannot use a simple XMLHttpRequest.

I have no code so far. I’m stuck on just the api request.
The only constraint is I can’t add anything. I’ve tried fetch, get, etc. All return undefined .
Is it me or the test?

I don’t see why they would return undefined unless you made some mistake in (what I assume is) your js script.

Also, what do you mean by test?
Are you running your code in a tested environment? That may change things up.

It’s difficult to help if we do not have a clear picture.

Sorry i’d like to give you a more clear picture. Their website makes it harder for me because they prevent copy and pasting
here’s the code they give me. The fetch() part is mine and returns undefined.

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}


/*
 * Complete the 'getUserTransaction' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. INTEGER uid
 *  2. STRING txnType
 *  3. STRING monthYear
 *
 *  https://jsonmock.hackerrank.com/api/transactions/search?txnType=
 */
async function getUserTransaction(uid, txnType, monthYear) {
fetch('https://jsonmock.hackerrank.com/api/transactions/search?txnType=')


}; 

Looks like you are using HackerRank :slight_smile:

If you see at the dropdown, you will see that you are using some version of node as language.

Well, if you go over the Node documentation you will find a native http (or https) method to perform requests:
here the documentation for Node v12 (refer to the one hackerrank is using)
https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_http_get_url_options_callback

tldr:
http in node with NO error handling:

var http = require("http");

http.get('URL', (res) => {
res.setEncoding("utf8");
  let body = "";
  res.on("data", data => {
    body += data;
  });
  res.on("end", () => {
    body = JSON.parse(body);
    console.log(body);
  });
});

4 Likes

Thanks for the help. I’m still getting the error, I think this code test might be a little over my head. I really appreciate you taking the time out of your day to help me though.

How would you make another request inside this one?
For example, to manipulate the information, I have to write my loops and/or if-statements, inside res.on(“end”, ( ) => { });
But with the example given for the challenge, you have to get a response first to know how many pages you have to look through. Then you have to make another GET request to get all transactions from all pages on the API.
I was wondering how you would do that.

If it’s an option, I would wrap the get request inside a Promise and use Promise.all to chain all the request together.

This is an example, it lacks proper error handling but should suffice for the demonstration:

// wrap your https request in a Promise
const asPropmise = (url) => new Promise((resolve, reject) => {
  https.get(url, (res) => {
res.setEncoding("utf8");
  let body = "";
  res.on("data", data => {
    body += data;
  });
  res.on("end", () => {
    body = JSON.parse(body);
    resolve(body);
  });
});
})
// consume your promise
Promise.all([asPropmise(url1), asPropmise(url2), asPropmise(url3)])
.then(res => console.log(res)) // an array of responses

Hope this helps :slight_smile:

Monique, generally its better to start your own thread or send him a private message. Please don’t revive an 8 month old thread.