HttpService
Send web requests from your program: fetch a page, post JSON, read any answer's status and headers.
FetchAsync gives any answer, with Roblox's StatusCode, StatusMessage, Success, Headers and Body, and nil and why only when nothing answered. GetTextAsync and PostTextAsync give the body of a 2xx answer, else nil and why. The names differ from Roblox's on purpose: Roblox's throw on a failure, these return nil and why. The service never prints or logs a request or its answer.
A service: write use HttpService.
Example
-- Talks to a small server of its own on this computer (a PageService route), never the internet.
use HttpService
use PageService
local server, why = PageService:ServeAsync()
if not server then
error("couldn't start the server: " .. tostring(why))
end
local notes : {string} = { "buy milk" }
server:Handle("/api/notes", function(request : PageRequest) : PageResponse
if request.Method == "POST" then
table.insert(notes, request.Body)
return { Status = 201, Body = "saved note " .. #notes }
end
return { Body = table.concat(notes, ", ") }
end)
-- a route's address goes before the server's key (Url ends in ?k=...)
local function at(route : string) : string
return (string.gsub(server.Url, "/%?", "/" .. route .. "?", 1))
end
print("PostTextAsync: " .. tostring((HttpService:PostTextAsync(at("api/notes"), "call home", "text/plain"))))
print("GetTextAsync: " .. tostring((HttpService:GetTextAsync(at("api/notes")))))
local answer = HttpService:FetchAsync({ Url = at("nowhere") })
print("FetchAsync of a missing page: " .. (if answer then answer.StatusCode .. " " .. answer.StatusMessage .. ", Success " .. tostring(answer.Success) else "no answer"))
print("GetTextAsync of a missing page: " .. tostring((HttpService:GetTextAsync(at("nowhere")))))
server:Destroy()PostTextAsync: saved note 2 GetTextAsync: buy milk, call home FetchAsync of a missing page: 404 Not Found, Success false GetTextAsync of a missing page: nil
Methods
| Name | Arguments | Returns | Description |
|---|---|---|---|
| FetchAsync yields | (request: HttpRequest) | (HttpResponse?, string?) | Waits for the answer to a request, any status included, with StatusCode, StatusMessage, Success, Headers and Body; nil and why only when nothing answered. |
| GetTextAsync yields | (url: string, headers: { [string]: string }?) | (string?, string?) | Waits for the body of a GET answered with a 2xx status; any other status gives nil and why with the body's start. |
| PostTextAsync yields | (url: string, body: string, contentType: string?, headers: { [string]: string }?) | (string?, string?) | Waits for the body of a POST's 2xx answer; contentType is application/json when nil, and other statuses give nil and why. |
From ShotInstance 33
ClassNameNameParentAncestryChangedAttributeChangedChangedChildAddedChildRemovedDestroyingAddTagClearAllChildrenCloneDestroyFindFirstAncestorFindFirstAncestorOfClassFindFirstAncestorWhichIsAFindFirstChildFindFirstChildOfClassFindFirstChildWhichIsAGetAttributeGetAttributeChangedSignalGetAttributesGetChildrenGetDescendantsGetFullNameGetPropertyChangedSignalGetTagsHasTagIsAIsAncestorOfIsDescendantOfRemoveTagSetAttribute
