HttpService for Dummies


  • HttpService for Dummies
    Difficulty Level: 4/5 Stars

    Hey everyone, I'm back with another tutorial. HttpService is a concept that's easy to grasp but takes practice. I put the difficulty level at 4/5 stars because it depends on what API -- Application Programming Interface -- you use. For example there's a Google API Library, Oxford Dictionary API, and the list goes on. These can all be used inside of roblox.

    In this tutorial we'll be using a News API. Once at the News API link you should click on View All and then press Google News, or click here to access it. Now, press Get API Key. It will make you sign up, and go through a process. Once you're done going through the process it should give you a bunch of random letters and numbers. This is your API Key. Write it down so you don't forget it.

    Now, this is an extremely important link you're going to use:

    https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=API_KEY

    Go into roblox studio and create a server sided script inside of ServerScriptService. Open up the script and type:

    local http = game:GetService("HttpService")
    local url = "https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=API_KEY"
    local data = http:GetAsync(url, false) -- Sends out a request to the url and then returns some information. This information is a giant table in JSON.
    local decodedData = http:JSONDecode(data) -- Uses the method JSONDecode in order to turn the data from JSON format into lua format.
    print(decodedData.articles[1].title) -- That will print the title of the first article.
    

    Replace API_KEY in the url with your API Key.

    Press Play and in your output you should see the title of an article.

    You can also do:

    print(decodedData.articles[1].description) -- Prints the article description.
    

    or

    print(decodedData.articles[1].author) -- Prints the author of the article.
    

    You can advance this with basic lua like for i,v in pairs() loops and use GUIs. I ended up creating a GUI newspaper:

    0_1490389704361_newspaperIMG.png

    As you can see, this works very well. You can use different APIs and follow their online tutorials to do this with dictionaries, translators, and even voice modules!

    Hope I explained this well, and be sure to give me some feedback! :)


  • I keep getting "ServerScriptService.Script:2: '<name>' expected near '/'" in the output.


  • The URL should be a string.

  • Global Moderator

    I'd still rate that about 1 or 2/5 stars.


  • @potatoboy99990 So sorry, I made a small mistake! The URL should be a string!
    @Link150 :( Why only 2 stars?


  • All http request should be in a pcall so that you can react in the event of an error.

  • Global Moderator

    @mccrafter1212 I meant the difficulty, not the quality of your tutorial.

Log in to reply
 

Looks like your connection to Scripting Helpers was lost, please wait while we try to reconnect.