How to use HTTP service?

Hi i want to use HTTP Service to get questions from an API key and display it along with answers in a multiple choice style fashion.

The website is opentdb and they have lots of questions and answers that are multiple choice. I need help making this transfer into roblox so i can use it.

Documentation I read : opentdb 0.1.0 - Docs.rs
The website i generated API key from : Open Trivia DB: Free to use, user-contributed trivia question database.

I still don’t know how to do it so can explain to me how i can do this, Thanks.

1 Like

Did you click “API DOCUMENTATION” in your second link? Read that, it seems pretty straightfoward.

The ROBLOX part is easy, too.

Make GET requests with GetAsync and parse the resulting JSONDecode to get a lua table. Look through it for your questions.

If i JSONDecode it is it just a table ?

Yes, and it will probably look something like their example request (https://opentdb.com/api.php?amount=10).

This returned this for me (I made it pretty):

{
   "response_code":0,
   "results":[
      {
         "category":"Science & Nature",
         "type":"multiple",
         "difficulty":"hard",
         "question":"How many objects are equivalent to one mole?",
         "correct_answer":"6.022 x 10^23",
         "incorrect_answers":[
            "6.002 x 10^22",
            "6.022 x 10^22",
            "6.002 x 10^23"
         ]
      },
      {
         "category":"Entertainment: Books",
         "type":"multiple",
         "difficulty":"hard",
         "question":"What is Hermione Granger's middle name?",
         "correct_answer":"Jean",
         "incorrect_answers":[
            "Jane",
            "Emma",
            "Jo"
         ]
      },
      {
         "category":"History",
         "type":"multiple",
         "difficulty":"medium",
         "question":"What year did the Battle of Agincourt take place?",
         "correct_answer":"1415",
         "incorrect_answers":[
            "1463",
            "1401",
            "1422"
         ]
      },
      {
         "category":"Mythology",
         "type":"boolean",
         "difficulty":"medium",
         "question":"According to Norse mythology, Loki is a mother.",
         "correct_answer":"True",
         "incorrect_answers":[
            "False"
         ]
      },
      {
         "category":"Entertainment: Video Games",
         "type":"multiple",
         "difficulty":"easy",
         "question":"When was Minecraft first released to the public?",
         "correct_answer":"May 17th, 2009",
         "incorrect_answers":[
            "September 17th, 2009",
            "November 18th, 2011",
            "October 7th, 2011"
         ]
      },
      {
         "category":"Entertainment: Film",
         "type":"multiple",
         "difficulty":"hard",
         "question":"In what Disney movie can you spot the character "Pac-Man" in if you look closely enough in some scenes?",
         "correct_answer":"Tron",
         "incorrect_answers":[
            "Big Hero 6",
            "Fantasia",
            "Monsters, Inc."
         ]
      },
      {
         "category":"Entertainment: Music",
         "type":"multiple",
         "difficulty":"easy",
         "question":"What album did The Lumineers release in 2016?",
         "correct_answer":"Cleopatra",
         "incorrect_answers":[
            "Winter",
            "The Lumineers",
            "Tracks From The Attic"
         ]
      },
      {
         "category":"Entertainment: Television",
         "type":"multiple",
         "difficulty":"hard",
         "question":"What was the callsign of Commander William Adama in Battlestar Galactica (2004)?",
         "correct_answer":"Husker",
         "incorrect_answers":[
            "Starbuck",
            "Apollo",
            "Crashdown"
         ]
      },
      {
         "category":"Sports",
         "type":"multiple",
         "difficulty":"medium",
         "question":"Why was The Green Monster at Fenway Park was originally built?",
         "correct_answer":"To prevent viewing games from outside the park.",
         "incorrect_answers":[
            "To make getting home runs harder.",
            "To display advertisements.",
            "To provide extra seating."
         ]
      },
      {
         "category":"Entertainment: Film",
         "type":"multiple",
         "difficulty":"medium",
         "question":"Who performed the opening theme song for the James Bond 007 movie "Goldfinger"?",
         "correct_answer":"Shirley Basey",
         "incorrect_answers":[
            "Tom Jones",
            "John Barry",
            "Sheena Easton"
         ]
      }
   ]
}

How did you do that can you show what code you wrote for it to return that ?
I wrote this:

local HttpService = game:GetService("HttpService")
local Link = "https://opentdb.com/api.php?amount=10"
local response = HttpService:GetAsync(Link)

local data = HttpService:JSONDecode(response)

print(data)

I didn’t write any code, I clicked on that link in my browser and copied the text that showed up.

Your code looks fine. Did you do this?

No let me enable it and see.

30 chars

Nothing is returning i switched on Http service and made it enabled. I still see nothing happen and it just lags my entire studio? Why does it do this?

It works for me.

  1. Is there an error in the output?
  2. Are you running this from a server script?
  3. When testing are you looking at the server, not the client?
  4. It should only print table: 0xfh3u1ih3h9. Do print(response) if you wanna see the text version

I see no errors my output is clear.

Oh it works when i print response but data returns {}

No, you just can’t print a table directly like that.
Try replacing your print statement with something like this:

print("Response code: " .. tostring(data.response_code))
print("Questions:")
for _, v in pairs(data.results) do
    print(v.question .. " -> " .. v.correct_answer)
end

Thanks! I don’t know where you are getting the v.something from is it from the documentation ?

Here’s a great detailed explanation, but I’ll try to summarize:

_ and v were just variable names I chose – but they were pretty bad ones, you’re right.

That’s just one way to step through the items in a table. Here’s a simpler example you should run:

local myTable = {"the", "quick", "brown", "fox"}

for theIndex, theValue in pairs(myTable) do
    print("At an index of " .. theIndex .. " the value is " .. theValue);
end

Since data.results is just a table like myTable is, I used the same pattern. However, I didn’t care about what the index was, so I named it “_” (a single underscore).

So inside the loop, v just meant, “one of the results”. Like:

for _, v in pairs(data.results) do
    --[[
    inside this loop the first time, v = {
         "category":"Science & Nature",
         "type":"multiple",
         "difficulty":"hard",
         "question":"How many objects are equivalent to one mole?",
         "correct_answer":"6.022 x 10^23",
         "incorrect_answers":[
            "6.002 x 10^22",
            "6.022 x 10^22",
            "6.002 x 10^23"
         ]
      }

    ... however, this loop will run 10 times and v will be a different
    result each time!
    --]]
    print(v.category)
    print(v.difficulty)
    -- you can even do another loop inside of this one!
    for _, incorrectAnswer in pairs(v.incorrect_answers) do
        print('WRONG ANSWER CHOICE: ' .. incorrectAnswer);
    end
end

Also, there are lots of ways to go through an array – you dont need to use for a, b, in pairs.

One common way is to just increment the index yourself and access the data at that index:

local myTable = {"the", "quick", "brown", "fox"}

for theIndex = 1, 4 do
    -- inside this loop, `theIndex` will be 1, and then 2, and then 3, and then 4.
    -- we can access the value ourself now
    local theValue = myTable[theIndex]

    print("Iteration number " .. theIndex .." -> myTable[" .. theIndex .. "] = " .. theValue);
end

In fact, some people would say that you should only ever use this method for arrays (e.g. {key1 = "the", key2 = "fox"}) because for i = 1, n is faster and more efficient for arrays.

In reality, it almost never matters. Do whichever is easier.

There’s even more functions you can swap out for pairs, too! But usually, nobody does anything except the two kinds of for loops I showed here.

Alright thank you i will bear that in mind!