HttpService
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สามารถส่งคำขอ HTTP จากเซิร์ฟเวอร์เกมโดยใช้ และ ได้บริการนี้ช่วยให้เกมสามารถผสานรวมกับบริการเว็บที่ไม่ใช่ Roblox เช่นการวิเคราะห์ข้อมูล การจัดเก็บข้อมูล การกำหนดค่าเซิร์ฟเวอร์ระยะไกล การรายงานข้อผิดพลาด การคำนวณขั้นสูงหรือการสื่อสารแบบเรียลไทม์นอกจากนี้ หลาย API เว็บของ Roblox ยังสามารถเข้าถึงได้ผ่านทาง HttpService (ดูด้านล่าง)
HttpService ยังมีวิธี JSONEncode และ JSONDecode ซึ่งเป็นประโยชน์สำหรับการสื่อสารกับบริการที่ใช้รูปแบบ JSONนอกจากนี้เมธอด GenerateGUID จะให้ฉลาก 128 บิตแบบสุ่มซึ่งสามารถปฏิบัติต่อได้เป็นเอกลักษณ์โดยบังเอิญในหลากหลายสถานการณ์
คุณควรส่งคำขอ HTTP เฉพาะไปยังแพลตฟอร์มบุคคลที่สามที่เชื่อถือได้เพื่อหลีกเลี่ยงการทำให้ประสบการณ์ของคุณเปราะบางต่อความเสี่ยงด้านความปลอดภัย
คุณสมบัตินี้ไม่สามารถโต้ตอบได้ในเวลาทำงาน
เปิดใช้งานคำขอ HTTP
วิธีการส่งคำขอไม่ได้เปิดใช้งานโดยค่าเริ่มต้น เพื่อส่งคำขอคุณต้อง เปิดใช้งานคำขอ HTTP สำหรับประสบการณ์ของคุณ
ใช้ในปลั๊กอิน
HttpService สามารถใช้โดยปลั๊กอินของ Studio ได้พวกเขาอาจทำเช่นนี้เพื่อตรวจสอบการอัปเดต ส่งข้อมูลการใช้งาน ดาวน์โหลดเนื้อหา หรือโลจิกส์ทางธุรกิจอื่นๆครั้งแรกที่ปลั๊กอินพยายามทำเช่นนี้ผู้ใช้อาจถูกขอให้ให้สิทธิ์แก่ปลั๊กอินในการสื่อสารกับที่อยู่เว็บเฉพาะผู้ใช้สามารถยอมรับ ปฏิเสธ และเพิกถอนสิทธิ์ดังกล่าวได้ตลอดเวลาผ่านหน้าต่าง การจัดการปลั๊กอิน ปลั๊กอินอาจสื่อสารกับซอฟต์แวร์อื่นที่ทำงานบนคอมพิวเตอร์เดียวกันผ่านเซิร์ฟเวอร์ localhost และ 127.0.0.1โดยการทำงานโปรแกรมที่สอดคล้องกับปลั๊กอินเหล่านี้คุณสามารถขยายฟังก์ชันของปลั๊กอินของคุณเกินความสามารถปกติของ Studio เช่นการโต้ตอบกับระบบไฟล์ของคอมพิวเตอร์ของคุณระวังว่าซอฟต์แวร์ดังกล่าวต้องแจกจ่ายแยกต่างหากจากปลั๊กอินเองและอาจเป็นอันตรายต่อความปลอดภัยหากคุณไม่ระวัง
เรียกโดเมน Roblox
HttpService สามารถเรียกส่วนหนึ่งของจุดสิ้นสุดเมฆเปิดได้เฉพาะในปัจจุบันเราวางแผนที่จะขยายชุดนี้ไปเรื่อยๆ ดังนั้นโปรดใช้ปุ่มฟีดแบ็คหากคุณมีจุดสิ้นสุดที่เฉพาะเจาะจงที่คุณต้องการดู:
กลุ่ม
ร้านข้อมูล
เนื่องจากข้อจำกัดปัจจุบันบน HttpService เฉพาะตัวอักษรและตัวอักษร - เท่านั้นที่อนุญาตในพารามิเตอร์เส้นทาง URL สำหรับโดเมน Robloxซึ่งหมายความว่าสโตร์ข้อมูล/รายการที่มีตัวอักษรพิเศษอื่นๆ ไม่สามารถเข้าถึงได้ในปัจจุบันจาก HttpService
ไอเทมสินค้าคงคลัง
ร้านค้าผู้สร้าง
คุณสามารถเรียกจุดสิ้นสุดเหล่านี้ได้เช่นเดียวกับที่คุณเรียกจุดสิ้นสุดอื่นผ่าน HttpServiceความแตกต่างเพียงอย่างเดียวคือคุณต้องรวมกุญแจ API เปิดระบบคลาวด์ในคำขอ:
- สร้างคำขอ (ตัวอย่างโค้ดด้านล่าง)
ข้อจำกัดรวมถึงสิ่งต่อไปนี้:
- อนุญาตเฉพาะหัวข้อ x-api-key และ content-type เท่านั้น
- หัวข้อ x-api-key ต้องเป็น Secret
- อนุญาตให้มีเฉพาะตัวอักษรและตัวเลขที่เป็นอักขระ - เท่านั้นในพารามิเตอร์เส้นทาง URL
- สนับสนุนเฉพาะโปรโตคอล https:// เท่านั้น
การจํากัดอัตรา
สำหรับแต่ละเซิร์ฟเวอร์เกม Roblox มีขีดจํากัดของ 500 คําขอ HTTP ต่อนาทีเกินขีดจำกัดนี้อาจทำให้วิธีการส่งคำขอหยุดทั้งหมดเป็นเวลาประมาณ 30 วินาทีไฟล์ pcall() ของคุณอาจล้มเหลวด้วยข้อความ "จํานวนคําขอเกินขีดจํากัด"
คําขอเปิดบนคลาวด์ใช้ขีดจํากัดรวมเดียวกันของ 500 คําขอ HTTP ต่อนาทีที่บังคับใช้กับคําขออื่น ๆ ทั้งหมด
แต่ละจุดสิ้นสุดเมฆเปิดมีขีดจํากัดของตัวเองต่อเจ้าของคีย์ API (สามารถเป็นผู้ใช้หรือกลุ่ม) ที่ถูกบังคับใช้ไม่ว่าจะมาจากที่ใด ( HttpService , เว็บ, ฯลฯ)หัวข้อต่อไปนี้จะถูกส่งกลับไปกับทุกคำตอบและช่วยให้คุณสามารถดูขีดจํากัดและความเหลือของคุณได้:
- x-ratelimit-limit - จํานวนคําขอทั้งหมดที่อนุญาตให้ทําต่อเจ้าของรหัส API (โดยปกติต่อนาที)
- x-ratelimit-remaining - จํานวนคําขอที่ใช้คีย์ API ยังคงสามารถทําได้หากหมายเลขนี้เป็น 0 และคุณได้รับรหัสสถานะการตอบสนอง HTTP 429 แล้วคุณจะถึงขีดจํากัดอัตราสําหรับจุดจบนี้
- x-ratelimit-reset - จํานวนวินาทีที่เหลือก่อนที่ขีดจํากัดอัตราจะรีเซ็ต (คือก่อนที่ x-ratelimit-remaining จะรีเซ็ตเป็น x-ratelimit-limit )
ข้อพิจารณาเพิ่มเติม
- มีข้อจำกัดด้านพอร์ตคุณไม่สามารถใช้พอร์ต 1194 หรือพอร์ตใดๆ ด้านล่าง 1024 ยกเว้น 80 และ 443หากคุณพยายามใช้พอร์ตที่ถูกบล็อกคุณจะได้รับข้อผิดพลาดเป็น 403 Forbidden หรือ ERR_ACCESS_DENIED
- คำขอเว็บสามารถล้มเหลวได้หลายเหตุผลดังนั้นจึงเป็นสิ่งสำคัญที่จะ "โค้ดป้องกัน" (ใช้ pcall()) และมีแผนสำหรับเมื่อคำขอล้มเหลว
- แม้ว่าโปรโตคอล http:// จะได้รับการสนับสนุน คุณควรใช้ https:// ทุกที่ที่เป็นไปได้
- คำขอที่ส่งควรให้การยืนยันแบบปลอดภัยเช่นกุญแจลับที่แชร์ล่วงหน้าเพื่อให้ผู้กระทำผิดไม่สามารถแอบอ้างเป็นหนึ่งในเซิร์ฟเวอร์เกม Roblox ของคุณได้
- ตระหนักถึงความสามารถทั่วไปและนโยบายจํากัดอัตราของเซิร์ฟเวอร์เว็บที่คําขอถูกส่งไปยัง
ตัวอย่างโค้ด
ตัวอย่างโค้ดนี้ใช้ HttpService's GetAsync เพื่อส่งคำขอไปยัง Open Notify บริการเว็บที่ให้ข้อมูลจาก NASAคำขอถูกส่งไปยังจุดสิ้นสุดที่ให้ข้อมูลเกี่ยวกับจํานวนนักบินอวกาศที่อยู่ในอวกาศในปัจจุบันการตอบสนองจะมาในรูปแบบ JSON ดังนั้นจึงถูกแยกใช้โดยใช้ JSONDecodeในที่สุดข้อมูลตอบสนองจะถูกประมวลผลและพิมพ์ลงในเอาต์พุต
ทดสอบสคริปต์นี้โดยการวางโค้ดที่มาจากแหล่งข้อมูลลงในสคริปต์ (HttpService ไม่สามารถใช้โดย LocalScripts)นอกจากนี้ ตรวจสอบให้แน่ใจว่าเปิดใช้งานคําขอ HTTP ในการตั้งค่าเกมของคุณ (บ้าน > การตั้งค่าเกม)
local HttpService = game:GetService("HttpService")
local URL_ASTROS = "http://api.open-notify.org/astros.json"
-- ส่งคำขอไปยัง URL จุดสิ้นสุดของเรา
local response = HttpService:GetAsync(URL_ASTROS)
-- แยกคำตอบ JSON
local data = HttpService:JSONDecode(response)
-- ข้อมูลในตารางข้อมูลขึ้นอยู่กับคำตอบ JSON
if data.message == "success" then
print("There are currently " .. data.number .. " astronauts in space:")
for i, person in pairs(data.people) do
print(i .. ": " .. person.name .. " is on " .. person.craft)
end
end
This code sample uses HttpService's GetAsync to make a request to an endpoint at Open Notify, a website that provides information from NASA. The endpoint provides information on the current location of the International Space Station. This example uses a defensive coding technique that you should use when making web requests. It wraps the call to GetAsync and JSONDecode in pcall, which protects our script from raising an error if either of these fail. Then, it checks the raw response for all proper data before using it. All of this is put inside a function that returns true or false depending of the request's success.
Whenever you're working with web requests, you should prepare for anything to go wrong. Perhaps your web endpoint changes or goes down - you don't want your game scripts raising errors and breaking your game. You want to handle both success and failure gracefully - have a plan in case your data is not available. Use pcall and make plenty of validity checks (if statements) on your data to make sure you're getting exactly what you expect.
local HttpService = game:GetService("HttpService")
-- Where is the International Space Station right now?
local URL_ISS = "http://api.open-notify.org/iss-now.json"
local function printISS()
local response
local data
-- Use pcall in case something goes wrong
pcall(function()
response = HttpService:GetAsync(URL_ISS)
data = HttpService:JSONDecode(response)
end)
-- Did our request fail or our JSON fail to parse?
if not data then
return false
end
-- Fully check our data for validity. This is dependent on what endpoint you're
-- to which you're sending your requests. For this example, this endpoint is
-- described here: http://open-notify.org/Open-Notify-API/ISS-Location-Now/
if data.message == "success" and data.iss_position then
if data.iss_position.latitude and data.iss_position.longitude then
print("The International Space Station is currently at:")
print(data.iss_position.latitude .. ", " .. data.iss_position.longitude)
return true
end
end
return false
end
if printISS() then
print("Success")
else
print("Something went wrong")
end
Pastebin.com is a website that allows users to paste text (usually source code) for others to view publicly. This code sample uses HttpService PostAsync and the pastebin web API to automatically create a new public paste on the website. Since pastebin's API is designed to take data in as a URL encoded string, the code uses a for-loop to turn the dataFields table into a URL encoded string, such as hello=world&foo=bar. This is used as the HTTP POST data.
Test this code by first going to pastebin.com/api#1 and getting an API key (you'll need a pastebin account to do this). Then, paste your unique developer API key into the field api_dev_key in the code sample's dataFields table. Fill in any other information about the post you want to make, then run this code in a Script (not a LocalScript). If all goes well, you'll get a URL to your new paste in the Output window (or some error string from pastebin).
local HttpService = game:GetService("HttpService")
local URL_PASTEBIN_NEW_PASTE = "https://pastebin.com/api/api_post.php"
local dataFields = {
-- Pastebin API developer key from
-- https://pastebin.com/api#1
["api_dev_key"] = "FILL THIS WITH YOUR API DEVELOPER KEY",
["api_option"] = "paste", -- keep as "paste"
["api_paste_name"] = "HttpService:PostAsync", -- paste name
["api_paste_code"] = "Hello, world", -- paste content
["api_paste_format"] = "text", -- paste format
["api_paste_expire_date"] = "10M", -- expire date
["api_paste_private"] = "0", -- 0=public, 1=unlisted, 2=private
["api_user_key"] = "", -- user key, if blank post as guest
}
-- The pastebin API uses a URL encoded string for post data
-- Other APIs might use JSON, XML or some other format
local data = ""
for k, v in pairs(dataFields) do
data = data .. ("&%s=%s"):format(HttpService:UrlEncode(k), HttpService:UrlEncode(v))
end
data = data:sub(2) -- Remove the first &
-- Here's the data we're sending
print(data)
-- Make the request
local response = HttpService:PostAsync(URL_PASTEBIN_NEW_PASTE, data, Enum.HttpContentType.ApplicationUrlEncoded, false)
-- The response will be the URL to the new paste (or an error string if something was wrong)
print(response)
This code sample demonstrates sending a single HTTP PATCH request with JSON data to the Open Cloud Update Group Membership endpoint. Every Open Cloud endpoint requires adding your API key to the "x-api-key" header to receive a successful response. The generated API key must be stored as a Secret that can later be retrieved with HttpService:GetSecret("API KEY SECRET NAME").
-- Remember to set enable HTTP Requests in game settings!
local HttpService = game:GetService("HttpService")
local groupId = "your_group_id"
local membershipId = "your_membership_id"
local roleId = "your_role_id"
local function request()
local response = HttpService:RequestAsync({
Url = `https://apis.roblox.com/cloud/v2/groups/{groupId}/memberships/{membershipId}`, -- Updates a user's group membership
Method = "PATCH",
Headers = {
["Content-Type"] = "application/json", -- When sending JSON, set this!
["x-api-key"] = HttpService:GetSecret("APIKey"), -- Set in Creator Hub
},
Body = HttpService:JSONEncode({ role = `groups/{groupId}/roles/{roleId}` }),
})
if response.Success then
print("The response was successful:", response.StatusCode, response.StatusMessage)
else
print("The response returned an error:", response.StatusCode, response.StatusMessage)
end
print("Response body:\n", response.Body)
print("Response headers:\n", HttpService:JSONEncode(response.Headers))
end
-- Remember to wrap the function in a 'pcall' to prevent the script from breaking if the request fails
local success, message = pcall(request)
if not success then
print("The Http request failed to send:", message)
end
สรุป
คุณสมบัติ
บ่งบอกว่าคำขอ HTTP สามารถส่งไปยังเว็บไซต์ภายนอกได้หรือไม่
วิธีการ
สร้างสตริงสุ่ม UUID/GUID ได้อย่างอิสระโดยมีเครื่องหมายขีดข่วนได้ตามต้องการ
ส่งคืน Secret จากร้านค้าความลับ
ถอดรหัสสตริง JSON เป็นตาราง Luau
สร้างสตริง JSON จากตาราง Luau
แทนที่ตัวอักษรที่ไม่ปลอดภัยด้วยตัวอักษร "%" และตัวอักษรหกตัวทศนิยม
ส่งคำขอ HTTP GET
- PostAsync(url : Variant,data : string,content_type : Enum.HttpContentType,compress : boolean,headers : Variant):string
ส่งคำขอ HTTP POST
ส่งคำขอ HTTP โดยใช้วิธีการ HTTP ใดๆ ที่ให้สารบัญของข้อมูล
คุณสมบัติ
HttpEnabled
เมื่อตั้งค่าเป็น true , สคริปต์สามารถส่งคำขอไปยังเว็บไซต์โดยใช้ HttpService:GetAsync() , HttpService:PostAsync() และ HttpService:RequestAsync() ได้
คุณสมบัตินี้ต้องเปิดใช้งานผ่านอินเทอร์เฟซ การตั้งค่าเกม ใน Studio หรือสำหรับประสบการณ์ที่ไม่เผยแพร่โดยการตั้งค่าคุณสมบัตินี้เป็น โดยใช้แถบคําสั่ง :
game:GetService("HttpService").HttpEnabled = true
วิธีการ
GenerateGUID
วิธีนี้สร้างสตริงตัวระบุที่ไม่ซ้ำกันทั่วไปแบบสุ่ม UIDD (UIDD)สิบหกบิตของ UUID จะถูกแทนที่เป็นตัวเลขสิบหกทศนิยม (ฐาน 16) สามสิบสองตัว ที่แสดงในห้ากลุ่มที่แยกกับตัวอักษรในรูปแบบ 8-4-4-4-12 สำหรับตัวอักษรทั้งหมด 36 ตัว เช่น 123e4567-e89b-12d3-a456-426655440000
ข้อกำหนด UUID ที่ใช้คือ เวอร์ชัน 4 (สุ่ม) และตัวแปร 1 (DCE 1.1, ISO/IEC 11578:1996)UID ของรุ่นนี้ที่ใช้บ่อยที่สุดเนื่องจากความเรียบง่ายเนื่องจากสร้างแบบสุ่มทั้งหมดโปรดทราบว่าเวอร์ชันนี้ไม่มีคุณลักษณะบางอย่างที่เวอร์ชัน UUID อื่นมี เช่น เวลาที่บันทึกได้รับการเข้ารหัส ที่อยู่ MAC หรือการจัดเรียงตามเวลาเช่น UUIDv7 หรือ ULID
มีมากกว่า 5.3×10 36 UUIDs ที่ไม่ซ้ำกัน v4 ในซึ่งโอกาสในการหาซ้ำภายใน 103 ล้าน UUIDs คือหนึ่งในพัน
อาร์กิวเมนต์ wrapInCurlyBraces กำหนดว่าสตริงที่ส่งคืนถูกห่อหุ้มด้วยเครื่องหมายเชิงเส้น ( {} ) หรือไม่ ตัวอย่างเช่น:
- true : {94b717b2-d54f-4340-a504-bd809ef5bf5c}
- false : db454790-7563-44ed-ab4b-397ff5df737b
พารามิเตอร์
ว่าสตริงที่ส่งคืนควรถูกห่อหุ้มด้วยเครื่องหมายเล็บ ( {} ).
ส่งค่ากลับ
UID ที่สร้างแบบสุ่ม
ตัวอย่างโค้ด
This example uses HttpService's GenerateGUID method to generate and print a universally unique identifier.
local HttpService = game:GetService("HttpService")
local result = HttpService:GenerateGUID(true)
print(result) --> Example output: {4c50eba2-d2ed-4d79-bec1-02a967f49c58}
GetSecret
วิธีนี้จะคืนค่าที่เพิ่มไปแล้วก่อนหน้านี้ในร้านความลับสำหรับประสบการณ์เนื้อหาลับไม่สามารถพิมพ์ได้และไม่สามารถใช้งานได้เมื่อประสบการณ์ทำงานท้องถิ่น
การคืน Secret ที่ได้รับสามารถแปลงโดยใช้วิธีในตัวที่สร้างขึ้นเช่น Secret:AddPrefix() คาดว่าจะถูกส่งเป็นส่วนหนึ่งของคำขอ HTTP
สำหรับข้อมูลเพิ่มเติม ดูคู่มือการใช้งาน
พารามิเตอร์
ส่งค่ากลับ
JSONDecode
วิธีนี้เปลี่ยนวัตถุ JSON หรือคอลเลกชันเป็นตาราง Luau ด้วยลักษณะต่อไปนี้:
- กุญแจของตารางเป็นสตริงหรือตัวเลข แต่ไม่ทั้งสอง หากวัตถุ JSON มีทั้งสอง กุญแจสตริงจะถูกเพิกเฉย
- วัตถุ JSON ว่างเปล่าสร้างตาราง Luau ว่างเปล่า ( {} )
- หากสตริง input ไม่ใช่วัตถุ JSON ที่ถูกต้องวิธีนี้จะโยนข้อผิดพลาด
เพื่อเข้ารหัสตาราง Luau เป็นวัตถุ JSON ใช้วิธี HttpService:JSONEncode()
วิธีนี้สามารถใช้ได้ไม่ว่าคำขอ HTTP จะเปิดใช้งานหรือไม่
พารามิเตอร์
วัตถุ JSON ถูกถอดรหัส
ส่งค่ากลับ
วัตถุ JSON ถูกถอดรหัสเป็นตาราง Luau
ตัวอย่างโค้ด
This code sample gives an example JSON format string and parses it using HttpService's JSONDecode. It then verifies that the JSON was parsed correctly, and prints out some of the information within the object.
Try editing the JSON string to experiment with the format. Also experiment with inspecting the data in Lua to get comfortable with the Lua representation of the data (tables and other values).
local HttpService = game:GetService("HttpService")
local jsonString = [[
{
"message": "success",
"info": {
"points": 120,
"isLeader": true,
"user": {
"id": 12345,
"name": "JohnDoe"
},
"past_scores": [50, 42, 95],
"best_friend": null
}
}
]]
local data = HttpService:JSONDecode(jsonString)
if data.message == "success" then
-- Since tab["hello"] and tab.hello are equivalent,
-- you could also use data["info"]["points"] here:
print("I have " .. data.info.points .. " points")
if data.info.isLeader then
print("I am the leader")
end
print("I have " .. #data.info.past_scores .. " past scores")
print("All the information:")
for key, value in pairs(data.info) do
print(key, typeof(value), value)
end
end
JSONEncode
วิธีนี้เปลี่ยนตาราง Luau เป็นวัตถุ JSON หรือรายการเชิงเส้นตามเกณฑ์ต่อไปนี้:
กุญแจของตารางต้องเป็นตัวอักษรหรือตัวเลข หากตารางมีทั้งสองอย่าง รายการเลือกช่องจะได้รับความสำคัญ (กุญแจตัวอักษรจะถูกเพิกเฉย)
โต๊ะ Luau ว่างเปล่า ( {} ) สร้าง阵列 JSON ว่างเปล่า
ค่า nil ไม่เคยถูกสร้างขึ้น
การอ้างอิงตารางหมุนเวียนทำให้เกิดข้อผิดพลาด
วิธีนี้สามารถให้ค่าเช่น inf และ nan ซึ่งไม่ใช่ JSON ที่ถูกต้องอาจทำให้เกิดปัญหาหากคุณต้องการใช้ JSON ที่ออกมาที่อื่น
เพื่อย้อนกระบวนการเข้ารหัสและถอดรหัสวัตถุ JSON ใช้วิธี HttpService:JSONDecode()
วิธีนี้สามารถใช้ได้ไม่ว่าคำขอ HTTP จะเปิดใช้งานหรือไม่
พารามิเตอร์
ตาราง Luau ที่ใส่
ส่งค่ากลับ
สตริง JSON ที่ส่งคืน
ตัวอย่างโค้ด
This code sample turns a Lua table tab into a JSON string using HttpService's JSONEncode. Then, it prints out the string.
Try editing the Lua table to see how the JSON output changes.
local HttpService = game:GetService("HttpService")
local tab = {
-- Remember: these lines are equivalent
--["message"] = "success",
message = "success",
info = {
points = 123,
isLeader = true,
user = {
id = 12345,
name = "JohnDoe",
},
past_scores = { 50, 42, 95 },
best_friend = nil,
},
}
local json = HttpService:JSONEncode(tab)
print(json)
UrlEncode
วิธีนี้ เข้ารหัสเปอร์เซ็นต์ สตริงที่กำหนดไว้ให้ถูกเข้ารหัสอย่างถูกต้องด้วย % และตัวอักษร 16 ตัว
นี่มีประโยชน์เมื่อจัดรูปแบบ URL สำหรับใช้กับ HttpService:GetAsync() / HttpService:PostAsync() หรือ POST ข้อมูลประเภทสื่อ application/x-www-form-urlencoded ( Enum.HttpContentType.ApplicationUrlEncoded )
ตัวอย่างเช่น เมื่อคุณเข้ารหัส URL https://www.roblox.com/discover#/ วิธีนี้จะส่งคืน https%3A%2F%2Fwww%2Eroblox%2Ecom%2Fdiscover%23%2F
พารามิเตอร์
สตริง (URL) เพื่อเข้ารหัส
ส่งค่ากลับ
สตริงที่เข้ารหัส
ตัวอย่างโค้ด
ตัวอย่างโค้ดนี้ใช้ UrlEncode เพื่อแปลงสตริงเป็นสตริง เข้ารหัสร้อยละ ที่สามารถใช้ใน URL เป็นอาร์กิวเมนต์ได้สังเกตว่าเฉพาะตัวอักษรที่ไม่ได้จอง (ตัวอักษร ตัวเลข และ -_.~ ) เท่านั้นที่ไม่ถูกเปลี่ยนเป็นเทียบเท่าที่เข้ารหัสเปอร์เซ็นต์ตัวละครที่มีเอกลักษณ์ถูกเปลี่ยนรูปเช่นกัน (ตัวอย่างเช่น é ถูกเปลี่ยนเป็น %C3 )
local HttpService = game:GetService("HttpService")
local content = "Je suis allé au cinéma." -- ฝรั่งเศสสำหรับ "ฉันไปดูหนัง"
local result = HttpService:UrlEncode(content)
print(result) --> Je%20suis%20all%C3%A9%20au%20cinema%2E
Pastebin.com is a website that allows users to paste text (usually source code) for others to view publicly. This code sample uses HttpService PostAsync and the pastebin web API to automatically create a new public paste on the website. Since pastebin's API is designed to take data in as a URL encoded string, the code uses a for-loop to turn the dataFields table into a URL encoded string, such as hello=world&foo=bar. This is used as the HTTP POST data.
Test this code by first going to pastebin.com/api#1 and getting an API key (you'll need a pastebin account to do this). Then, paste your unique developer API key into the field api_dev_key in the code sample's dataFields table. Fill in any other information about the post you want to make, then run this code in a Script (not a LocalScript). If all goes well, you'll get a URL to your new paste in the Output window (or some error string from pastebin).
local HttpService = game:GetService("HttpService")
local URL_PASTEBIN_NEW_PASTE = "https://pastebin.com/api/api_post.php"
local dataFields = {
-- Pastebin API developer key from
-- https://pastebin.com/api#1
["api_dev_key"] = "FILL THIS WITH YOUR API DEVELOPER KEY",
["api_option"] = "paste", -- keep as "paste"
["api_paste_name"] = "HttpService:PostAsync", -- paste name
["api_paste_code"] = "Hello, world", -- paste content
["api_paste_format"] = "text", -- paste format
["api_paste_expire_date"] = "10M", -- expire date
["api_paste_private"] = "0", -- 0=public, 1=unlisted, 2=private
["api_user_key"] = "", -- user key, if blank post as guest
}
-- The pastebin API uses a URL encoded string for post data
-- Other APIs might use JSON, XML or some other format
local data = ""
for k, v in pairs(dataFields) do
data = data .. ("&%s=%s"):format(HttpService:UrlEncode(k), HttpService:UrlEncode(v))
end
data = data:sub(2) -- Remove the first &
-- Here's the data we're sending
print(data)
-- Make the request
local response = HttpService:PostAsync(URL_PASTEBIN_NEW_PASTE, data, Enum.HttpContentType.ApplicationUrlEncoded, false)
-- The response will be the URL to the new paste (or an error string if something was wrong)
print(response)
GetAsync
วิธีนี้ส่งคำขอ HTTP GETมันทำงานคล้ายกับ RequestAsync() ยกเว้นว่ามันรับพารามิเตอร์คำขอ HTTP เป็นพารามิเตอร์วิธีแทนคู่สารานุกรมเดียวและส่งคืนเฉพาะร่างกายของการตอบสนอง HTTP เท่านั้นโดยทั่วไปวิธีนี้มีประโยชน์เฉพาะในฐานะลักษณะย่อเท่านั้นและ RequestAsync() ควรใช้ในกรณีส่วนใหญ่
เมื่อ true , พารามิเตอร์ nocache ป้องกันไม่ให้วิธีนี้บันทึกผลลัพธ์จากการโทรก่อนหน้าด้วย url เดียวกัน
พารามิเตอร์
ที่อยู่เว็บที่คุณร้องขอข้อมูล
ว่าคำขอจะเก็บ (แคช) คำตอบหรือไม่
ใช้เพื่อระบุหัวข้อคำขอ HTTP บางอย่าง
ส่งค่ากลับ
ร่างคำตอบคำขอ GET
ตัวอย่างโค้ด
ตัวอย่างโค้ดนี้ใช้ HttpService's GetAsync เพื่อส่งคำขอไปยัง Open Notify บริการเว็บที่ให้ข้อมูลจาก NASAคำขอถูกส่งไปยังจุดสิ้นสุดที่ให้ข้อมูลเกี่ยวกับจํานวนนักบินอวกาศที่อยู่ในอวกาศในปัจจุบันการตอบสนองจะมาในรูปแบบ JSON ดังนั้นจึงถูกแยกใช้โดยใช้ JSONDecodeในที่สุดข้อมูลตอบสนองจะถูกประมวลผลและพิมพ์ลงในเอาต์พุต
ทดสอบสคริปต์นี้โดยการวางโค้ดที่มาจากแหล่งข้อมูลลงในสคริปต์ (HttpService ไม่สามารถใช้โดย LocalScripts)นอกจากนี้ ตรวจสอบให้แน่ใจว่าเปิดใช้งานคําขอ HTTP ในการตั้งค่าเกมของคุณ (บ้าน > การตั้งค่าเกม)
local HttpService = game:GetService("HttpService")
local URL_ASTROS = "http://api.open-notify.org/astros.json"
-- ส่งคำขอไปยัง URL จุดสิ้นสุดของเรา
local response = HttpService:GetAsync(URL_ASTROS)
-- แยกคำตอบ JSON
local data = HttpService:JSONDecode(response)
-- ข้อมูลในตารางข้อมูลขึ้นอยู่กับคำตอบ JSON
if data.message == "success" then
print("There are currently " .. data.number .. " astronauts in space:")
for i, person in pairs(data.people) do
print(i .. ": " .. person.name .. " is on " .. person.craft)
end
end
This code sample uses HttpService's GetAsync to make a request to an endpoint at Open Notify, a website that provides information from NASA. The endpoint provides information on the current location of the International Space Station. This example uses a defensive coding technique that you should use when making web requests. It wraps the call to GetAsync and JSONDecode in pcall, which protects our script from raising an error if either of these fail. Then, it checks the raw response for all proper data before using it. All of this is put inside a function that returns true or false depending of the request's success.
Whenever you're working with web requests, you should prepare for anything to go wrong. Perhaps your web endpoint changes or goes down - you don't want your game scripts raising errors and breaking your game. You want to handle both success and failure gracefully - have a plan in case your data is not available. Use pcall and make plenty of validity checks (if statements) on your data to make sure you're getting exactly what you expect.
local HttpService = game:GetService("HttpService")
-- Where is the International Space Station right now?
local URL_ISS = "http://api.open-notify.org/iss-now.json"
local function printISS()
local response
local data
-- Use pcall in case something goes wrong
pcall(function()
response = HttpService:GetAsync(URL_ISS)
data = HttpService:JSONDecode(response)
end)
-- Did our request fail or our JSON fail to parse?
if not data then
return false
end
-- Fully check our data for validity. This is dependent on what endpoint you're
-- to which you're sending your requests. For this example, this endpoint is
-- described here: http://open-notify.org/Open-Notify-API/ISS-Location-Now/
if data.message == "success" and data.iss_position then
if data.iss_position.latitude and data.iss_position.longitude then
print("The International Space Station is currently at:")
print(data.iss_position.latitude .. ", " .. data.iss_position.longitude)
return true
end
end
return false
end
if printISS() then
print("Success")
else
print("Something went wrong")
end
PostAsync
วิธีนี้ส่งคำขอ HTTP POSTมันทำงานคล้ายกับ RequestAsync() ยกเว้นว่ามันรับพารามิเตอร์คำขอ HTTP เป็นพารามิเตอร์วิธีแทนคู่สารานุกรมเดียวและส่งคืนเฉพาะร่างกายของการตอบสนอง HTTP เท่านั้นโดยทั่วไปวิธีนี้มีประโยชน์เฉพาะในฐานะลักษณะย่อเท่านั้นและ RequestAsync() ควรใช้ในกรณีส่วนใหญ่
เมื่อ true , พารามิเตอร์ compress จะควบคุมว่าร่างคำขอขนาดใหญ่จะถูกบีบอัดโดยใช้ gzip
พารามิเตอร์
ที่อยู่ปลายทางสำหรับข้อมูล
ข้อมูลที่ส่ง
แก้ไขค่าในหัวข้อ Content-Type ที่ส่งไปพร้อมกับคำขอ
กำหนดว่าข้อมูลจะถูกบีบอัด ( บีบอัด ) เมื่อส่ง
ใช้เพื่อระบุหัวข้อคำขอ HTTP บางอย่าง
ส่งค่ากลับ
คำตอบ HTTP ที่ส่งกลับบ่งบอกถึงผลลัพธ์คำขอ
ตัวอย่างโค้ด
Pastebin.com is a website that allows users to paste text (usually source code) for others to view publicly. This code sample uses HttpService PostAsync and the pastebin web API to automatically create a new public paste on the website. Since pastebin's API is designed to take data in as a URL encoded string, the code uses a for-loop to turn the dataFields table into a URL encoded string, such as hello=world&foo=bar. This is used as the HTTP POST data.
Test this code by first going to pastebin.com/api#1 and getting an API key (you'll need a pastebin account to do this). Then, paste your unique developer API key into the field api_dev_key in the code sample's dataFields table. Fill in any other information about the post you want to make, then run this code in a Script (not a LocalScript). If all goes well, you'll get a URL to your new paste in the Output window (or some error string from pastebin).
local HttpService = game:GetService("HttpService")
local URL_PASTEBIN_NEW_PASTE = "https://pastebin.com/api/api_post.php"
local dataFields = {
-- Pastebin API developer key from
-- https://pastebin.com/api#1
["api_dev_key"] = "FILL THIS WITH YOUR API DEVELOPER KEY",
["api_option"] = "paste", -- keep as "paste"
["api_paste_name"] = "HttpService:PostAsync", -- paste name
["api_paste_code"] = "Hello, world", -- paste content
["api_paste_format"] = "text", -- paste format
["api_paste_expire_date"] = "10M", -- expire date
["api_paste_private"] = "0", -- 0=public, 1=unlisted, 2=private
["api_user_key"] = "", -- user key, if blank post as guest
}
-- The pastebin API uses a URL encoded string for post data
-- Other APIs might use JSON, XML or some other format
local data = ""
for k, v in pairs(dataFields) do
data = data .. ("&%s=%s"):format(HttpService:UrlEncode(k), HttpService:UrlEncode(v))
end
data = data:sub(2) -- Remove the first &
-- Here's the data we're sending
print(data)
-- Make the request
local response = HttpService:PostAsync(URL_PASTEBIN_NEW_PASTE, data, Enum.HttpContentType.ApplicationUrlEncoded, false)
-- The response will be the URL to the new paste (or an error string if something was wrong)
print(response)
RequestAsync
วิธีนี้ส่งคำขอ HTTP โดยใช้สารบัญเพื่อระบุข้อมูลคำขอ เช่น ที่อยู่ URL วิธี หัวข้อ และข้อมูลร่างคำขอมันส่งคืนสารานุกรมที่อธิบายข้อมูลการตอบรับที่ได้รับตัวเลือก, คำขอสามารถถูกบีบอัดโดยใช้ Enum.HttpCompression .
ขอฟิลด์สารานุกรม
<th>ประเภท</th><th>จำเป็น</th><th>คําอธิบาย</th></tr></thead><tbody><tr><td><code>ที่อยู่ URL</code></td><td>สตริง</td><td>ใช่</td><td>URL เป้าหมายสำหรับคำขอนี้ ต้องใช้โปรโตคอล <code>http</code> หรือ <code>https</code></td></tr><tr><td><code>วิธี</code></td><td>สตริง</td><td>no</td><td>วิธี HTTP ที่ใช้โดยคำขอนี้มักจะเป็น <code>รับ</code> หรือ <code>โพสต์</code></td></tr><tr><td><code>หัวข้อ</code></td><td>สารานุกรม</td><td>no</td><td>สารบัญของหัวข้อที่จะใช้กับคำขอนี้ ส่วนใหญ่ของหัวข้อ HTTP ได้รับการยอมรับที่นี่ แต่ไม่ทั้งหมด</td></tr><tr><td><code>ร่างกาย</code></td><td>สตริง</td><td>no</td><td>ร่างคำขอสามารถเป็นข้อความใดก็ได้รวมถึงข้อมูลไบนารีต้องถูกยกเว้นเมื่อใช้วิธีการ <code>รับ</code> หรือ <code>หัว</code> HTTP เมธอดอาจจำเป็นต้องระบุหัวข้อ <code>ประเภทเนื้อหา</code> เมื่อส่ง JSON หรือรูปแบบอื่น</td></tr><tr><td><code>บีบอัด</code></td><td><code>Enum.HttpCompression</code></td><td>no</td><td>ฟิลด์การบีบอัดที่เลือกได้ที่จะบีบอัดข้อมูลในคำขอค่าสามารถเป็น <code>Enum.HttpCompression.None</code> หรือ <code>Enum.HttpCompression.Gzip</code> ได้</td></tr></tbody>
ชื่อ |
---|
วิธี HTTP ที่สนับสนุน
วิธีการร้องขอ HTTP ระบุวัตถุประสงค์ของคำขอที่ทำและสิ่งที่คาดว่าจะเกิดขึ้นหากคำขอสําเร็จตัวอย่างเช่นวิธีการร้องขอ GET จะบอกเซิร์ฟเวอร์ที่ที่อยู่ที่ร้องขอว่ามีการร้องขอทรัพยากรและหากสําเร็จทรัพยากรที่อยู่นั้นจะถูกส่งคืนเช่นเดียวกับวิธีการร้องขอ HEAD ทำเช่นเดียวกันยกเว้นเซิร์ฟเวอร์รู้ว่าจะส่งคืนคำตอบโดยไม่มีองค์ประกอบ Body
<th>คําอธิบาย</th><th>ปลอดภัย</th></tr></thead><tbody><tr><td><code>รับ</code><a href="https://developer.mozilla.org/docs/Web/HTTP/Methods/GET">ⓘ</a></td><td>วิธี <code>รับ</code> จะขอทรัพยากรที่อยู่ที่ระบุ ไม่สนับสนุนการใช้พารามิเตอร์ <code>Body</code></td><td>ใช่</td></tr><tr><td><code>หัว</code><a href="https://developer.mozilla.org/docs/Web/HTTP/Methods/HEAD">ⓘ</a></td><td>วิธี <code>หัว</code> จะร้องขอการตอบสนองที่เหมือนกับคำขอ <code>รับ</code> แต่ไม่มีร่างตอบสนองไม่สนับสนุนการใช้พารามิเตอร์ <code>Body</code></td><td>ใช่</td></tr><tr><td><code>โพสต์</code><a href="https://developer.mozilla.org/docs/Web/HTTP/Methods/POST">ⓘ</a></td><td>วิธี <code>โพสต์</code> ส่งข้อมูล <code>ร่างกาย</code> ที่จัดเตรียมไปยังที่อยู่ที่ร้องขอ</td><td>No</td></tr><tr><td><code>ใส่</code><a href="https://developer.mozilla.org/docs/Web/HTTP/Methods/PUT">ⓘ</a></td><td>วิธี <code>PUT</code> จะแทนที่การเลื่อนทั้งหมดในปัจจุบันของทรัพยากรที่ระบุไว้ภายในข้อมูล <code>Body</code> ที่จัดหา</td><td>No</td></tr><tr><td><code>ลบ</code><a href="https://developer.mozilla.org/docs/Web/HTTP/Methods/DELETE">ⓘ</a></td><td>วิธี <code>ลบ</code> จะลบทรัพยากรที่ระบุในข้อมูล <code>Body</code> ที่จัดส่งที่ที่อยู่ที่ร้องขอ</td><td>No</td></tr><tr><td><code>ตัวเลือก</code><a href="https://developer.mozilla.org/docs/Web/HTTP/Methods/OPTIONS">ⓘ</a></td><td>วิธี <code>ตัวเลือก</code> จะขอตัวเลือกการสื่อสารที่อนุญาตสำหรับที่อยู่ที่จัดหา</td><td>ใช่</td></tr><tr><td><code>ติดตาม</code><a href="https://developer.mozilla.org/docs/Web/HTTP/Methods/TRACE">ⓘ</a></td><td>วิธี <code>ติดตาม</code> ทำการทดสอบลูปกลับข้อความตามเส้นทางไปยังทรัพยากรที่ระบุไว้ในข้อมูล <code>ร่างกาย</code> ที่จัดหา</td><td>ใช่</td></tr><tr><td><code>แพทช์</code><a href="https://developer.mozilla.org/docs/Web/HTTP/Methods/PATCH">ⓘ</a></td><td>วิธี <code>PATCH</code> ใช้การเปลี่ยนแปลงบางส่วนให้กับทรัพยากรที่ระบุไว้ในข้อมูล <code>Body</code> ที่จัดส่งที่ที่อยู่ที่ร้องขอ</td><td>No</td></tr></tbody>
วิธี |
---|
หัวข้อ HTTP
ในสารบัญคำขอ คุณสามารถระบุหัวข้อ HTTP ที่กําหนดเองที่จะใช้ในคําขอได้อย่างไรก็ตาม ไม่สามารถระบุหัวข้อบางอย่างได้ตัวอย่างเช่น Content-Length จะถูกกำหนดจากร่างคำขอUser-Agent และ Roblox-Id ถูกล็อคโดย Robloxหัวข้ออื่นๆ เช่น Accept หรือ Cache-Control ใช้ค่าเริ่มต้น แต่สามารถแทนที่ได้ทั่วไปมากขึ้นบาง API REST อาจต้องใช้คีย์ API หรือการยืนยันบริการอื่นเพื่อระบุในหัวข้อคำขอ
วิธี RequestAsync() ไม่ตรวจจับรูปแบบเนื้อหาบอดี้เซิร์ฟเวอร์เว็บจํานวนมากต้องการให้หัวข้อ Content-Type ถูกตั้งค่าอย่างเหมาะสมเมื่อส่งรูปแบบบางอย่างวิธีการอื่นของ ใช้เอนเนม สำหรับวิธีนี้ตั้งค่าหัวข้อ อย่างเหมาะสม: , , , หรือ เป็นค่าหัวข้อเอนเนมแทนที่สำหรับค่าเอนเนมตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับตามลำดับ
ฟิลด์สารบัญการตอบสนอง
RequestAsync() ส่งคืนสารานุกรมที่มีฟิลด์ต่อไปนี้:
<th>ประเภท</th><th>คําอธิบาย</th></tr></thead><tbody><tr><td><code>สําเร็จ</code></td><td>บูลีน</td><td>สถานะความสําเร็จของคําขอ นี่เป็นจริงถ้าและเฉพาะถ้า <code>StatusCode</code> อยู่ภายในช่วง <code>200</code> - <code>299</code></td></tr><tr><td><code>รหัสสถานะ</code></td><td>ตัวเลข</td><td>รหัสตอบสนอง HTTP ที่ระบุสถานะของคำตอบ</td></tr><tr><td><code>สถานะข้อความ</code></td><td>สตริง</td><td>ข้อความสถานะที่ส่งกลับ</td></tr><tr><td><code>หัวข้อ</code></td><td>สารานุกรม</td><td>สารบัญของหัวข้อที่กำหนดไว้ในคำตอบนี้</td></tr><tr><td><code>ร่างกาย</code></td><td /><td>ร่างคำขอ (เนื้อหา) ที่ได้รับในคำตอบ</td></tr></tbody>
ชื่อ |
---|
กรณีข้อผิดพลาด
RequestAsync() เพิ่มข้อผิดพลาดหากเวลาตอบหมดหรือหากเซิร์ฟเวอร์เป้าหมายปฏิเสธคำขอหากบริการเว็บลงชั่วคราวเนื่องจากเหตุผลใดก็ตาม จะทำให้สคริปที่ใช้วิธีนี้หยุดทำงานทั้งหมดมักเป็นความคิดที่ดีที่จะห่อเรียกไปยังวิธีนี้ใน pcall() และจัดการกรณีความล้มเหลวอย่างสง่างามหากข้อมูลที่จำเป็นไม่พร้อมใช้งาน
ข้อจํากัด
ข้อจำกัดปัจจุบันสำหรับการส่งและรับคำขอ HTTP คือ 500 คำขอต่อนาที คำขอเกินขีดจำกัดนี้จะล้มเหลว
พารามิเตอร์
สารานุกรมที่มีข้อมูลที่จะร้องขอจากเซิร์ฟเวอร์ที่ระบุ
ส่งค่ากลับ
สารานุกรมที่มีข้อมูลการตอบสนองจากเซิร์ฟเวอร์ที่ระบุ
ตัวอย่างโค้ด
This code sample demonstrates sending a single HTTP POST request with JSON data to the website httpbin.org, a website that helps debug HTTP requests. Here, we send some JSON data by using HttpService:JSONEncode() and also setting the Content-Type header.
-- Remember to set enable HTTP Requests in game settings!
local HttpService = game:GetService("HttpService")
local function request()
local response = HttpService:RequestAsync({
Url = "http://httpbin.org/post", -- This website helps debug HTTP requests
Method = "POST",
Headers = {
["Content-Type"] = "application/json", -- When sending JSON, set this!
},
Body = HttpService:JSONEncode({ hello = "world" }),
})
if response.Success then
print("Status code:", response.StatusCode, response.StatusMessage)
print("Response body:\n", response.Body)
else
print("The request failed:", response.StatusCode, response.StatusMessage)
end
end
-- Remember to wrap the function in a 'pcall' to prevent the script from breaking if the request fails
local success, message = pcall(request)
if not success then
print("Http Request failed:", message)
end
This code sample demonstrates sending a single HTTP PATCH request with JSON data to the Open Cloud Update Group Membership endpoint. Every Open Cloud endpoint requires adding your API key to the "x-api-key" header to receive a successful response. The generated API key must be stored as a Secret that can later be retrieved with HttpService:GetSecret("API KEY SECRET NAME").
-- Remember to set enable HTTP Requests in game settings!
local HttpService = game:GetService("HttpService")
local groupId = "your_group_id"
local membershipId = "your_membership_id"
local roleId = "your_role_id"
local function request()
local response = HttpService:RequestAsync({
Url = `https://apis.roblox.com/cloud/v2/groups/{groupId}/memberships/{membershipId}`, -- Updates a user's group membership
Method = "PATCH",
Headers = {
["Content-Type"] = "application/json", -- When sending JSON, set this!
["x-api-key"] = HttpService:GetSecret("APIKey"), -- Set in Creator Hub
},
Body = HttpService:JSONEncode({ role = `groups/{groupId}/roles/{roleId}` }),
})
if response.Success then
print("The response was successful:", response.StatusCode, response.StatusMessage)
else
print("The response returned an error:", response.StatusCode, response.StatusMessage)
end
print("Response body:\n", response.Body)
print("Response headers:\n", HttpService:JSONEncode(response.Headers))
end
-- Remember to wrap the function in a 'pcall' to prevent the script from breaking if the request fails
local success, message = pcall(request)
if not success then
print("The Http request failed to send:", message)
end