Can't parse JSON

Hi there, I’m trying to learn API’s. But I got this issue, that I can’t seem to fix.

At line 20, it says that it can’t parse JSON. Here’s the script.

-- Server Script
local http = game:GetService('HttpService')

local api_key = '********************************' -- API key
local url = "https://developer.schiphol.nl/admin/applications/1409623844995"
local headers = {
	["Authorization"] = "Bearer " .. api_key,
}

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if message:lower() == "!getdata" then
			local response = http:RequestAsync({
				Url = url,
				Method = "GET",
				Headers = headers
			})

			if response.Success then
				local responseData = http:JSONDecode(response.Body)
				print(responseData)
			else
				print("Failed to make the request:", response.StatusMessage)
			end
		end
	end)
end)

I’m not sure if the url is correct though. I just copied the link of the tab.
Apart from that, what’s an application ID?

Thanks for reading, and thanks for your time.
Hope you can help me out :slight_smile:

1 Like

You should try printing out response.Body just to see what you actually received that wasn’t JSON.

2 Likes

Just tried it, got this:
image
Not sure if I’m doing it right though.
The command I used for it was print(response.Body)

1 Like

Sorry, I meant doing that in your script. Running it as a console command will not work, since the response variable doesn’t exist there.

Yeah, that’s what I thought. Anyways. I still get the: Error parsing JSON: Can’t parse JSON error. But I also get:

<!DOCTYPE html>
<html>
  <head>
-- Followed by a lot of other stuff

Ah - there’s your problem.

You seem to be accessing a webpage instead of an API.

You should check schiphol’s documentation—there should be a URL they tell you to use. I don’t have access to their API and don’t want to make an account, but ChatGPT tells me you’re looking for something that starts with https://api.schiphol.nl/ (e.g. https://api.schipol.nl/public-flights)

ChatGPT lies sometimes though, so check their documentation to confirm.

2 Likes

Ah, I see.

I’ve completely skipped that part of their documentation, because I couldn’t understand what they meant. I’ve changed the url to “https://api.schiphol.nl/public-flights/flights?includedelays=false&page=0&sort=%2BscheduleTime

No errors but I get this:
image

Are you sure you’re doing the authentication correctly? I found an example online that uses app_id and app_key headers instead of bearer tokens.

However roblox doesn’t let you put underscores in header keys so you could try app-id/app-key instead:

local headers = {
  ["app-id"]: 1409623844995,
  ["app-key"]: "*********************"
}

Not sure if that works either, though. Check schiphol’s docs for what kind of authentication they accept.

You might also need to try using Basic authentication instead of Bearer authentication, which I describe how to do here: How could I like my game to a private Github repository? - #4 by nicemike40

1 Like

This is data about the API. I also changed the script to this

local app_id = 'b25ac413'
local app_key = '*******************************' -- API key
local url = "https://api.schiphol.nl/public-flights/flights?includedelays=false&page=0&sort=%2BscheduleTime"
local headers = {
	["Accept"]= "application/json",
	["app-id"]= "b25ac413",
	["app-key"]= "*******************************"
	["ResourceVersion"]= "v4"
}

-- The rest is the same

Were you not supposed to put a comma (,) at the end of your app-key header value? Can’t see any issues apart from that right now.

Additionally, if you’re adding the app-id and app-key in the header anyway and not the variables at the top there, you can remove the top two lines of that header snippet you sent.

1 Like

It was the comma that was the issue, oh my god. It works now. I get this long response.body and eventually this


Thank you very much!!
I’m very grateful for your help, @xmthl and @nicemike40!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.