SkHttp can let you make an API or listen for websockets, this tutorial will show you how to make an API.
First, you are going to want to make a new HTTP server, to do it safely remember to stop the HTTP server when the script unloads, here is an example:
# Addons needed: SkHttp
on load:
set {server} to new http server with port 8000
start http {server}
on unload:
stop http {server}
delete {server}
Great! Now you have a running HTTP server! Next we are going to want to make a new endpoint, also known as an HTTP Context, here is an example:
# Addons needed: SkHttp
on load:
set {server} to new http server with port 1000
make endpoint using {server}:
method: "GET"
path: "server"
trigger:
add 1 to {amount}
respond with code 200 and message "{""amount"": %{amount}%}"
on unload:
stop http {server}
delete {server}
When done the endpoint should return something like this:
All that endpoint does is return the amount of the times the endpoint has been visited. But what about arguments? Here is an example of getting player information:
# Addons needed: SkHttp
on load:
set {server} to new http server with port 1000
make endpoint using {server}:
method: "GET"
path: "info"
trigger:
set {_name} to (first element of (full path of the exchange split at "/"))
set {_json} to new json object
if {_name} is empty:
put "message" and "No player name set" in {_json}
respond with code 400 and message {_json}
else:
set {_player} to ({_name} parsed as offline player)
put "name" and {_player}'s name in {_json}
put "uuid" and {_player}'s uuid in {_json}
respond with code 200 and message {_json}
start http {server}
on unload:
stop http {server}
delete {server}
When the endpoint is done, it should return something like this if there is an argument set:
This is just a tutorial on how to make a really simple API using SkHttp, you can make your own API using what ever you want.
You must be logged in to comment