HttpService

แสดงที่เลิกใช้งานแล้ว

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่

ไม่สามารถสร้าง
บริการ

สามารถส่งคำขอ HTTP จากเซิร์ฟเวอร์เกมโดยใช้ และ ได้บริการนี้ช่วยให้เกมสามารถผสานรวมกับบริการเว็บที่ไม่ใช่ Roblox เช่นการวิเคราะห์ข้อมูล การจัดเก็บข้อมูล การกำหนดค่าเซิร์ฟเวอร์ระยะไกล การรายงานข้อผิดพลาด การคำนวณขั้นสูงหรือการสื่อสารแบบเรียลไทม์

HttpService ยังมีวิธี JSONEncode และ JSONDecode ซึ่งเป็นประโยชน์สำหรับการสื่อสารกับบริการที่ใช้รูปแบบ JSONนอกจากนี้เมธอด GenerateGUID จะให้ฉลาก 128 บิตแบบสุ่มซึ่งสามารถปฏิบัติต่อได้เป็นเอกลักษณ์โดยบังเอิญในหลากหลายสถานการณ์

คุณควรส่งคำขอ HTTP เฉพาะไปยังแพลตฟอร์มบุคคลที่สามที่เชื่อถือได้เพื่อหลีกเลี่ยงการทำให้ประสบการณ์ของคุณเปราะบางต่อความเสี่ยงด้านความปลอดภัย

คุณสมบัตินี้ไม่สามารถโต้ตอบได้ในเวลาทำงาน

เปิดใช้งานคำขอ HTTP

วิธีการส่งคำขอไม่ได้เปิดใช้งานโดยค่าเริ่มต้น เพื่อส่งคำขอคุณต้อง เปิดใช้งานคำขอ HTTP สำหรับประสบการณ์ของคุณ

ใช้ในปลั๊กอิน

HttpService สามารถใช้โดยปลั๊กอินของ Studio ได้พวกเขาอาจทำเช่นนี้เพื่อตรวจสอบการอัปเดต ส่งข้อมูลการใช้งาน ดาวน์โหลดเนื้อหา หรือโลจิกส์ทางธุรกิจอื่นๆครั้งแรกที่ปลั๊กอินพยายามทำเช่นนี้ผู้ใช้อาจถูกขอให้ให้สิทธิ์แก่ปลั๊กอินในการสื่อสารกับที่อยู่เว็บเฉพาะผู้ใช้สามารถยอมรับ ปฏิเสธ และเพิกถอนสิทธิ์ดังกล่าวได้ตลอดเวลาผ่านหน้าต่าง การจัดการปลั๊กอิน ปลั๊กอินอาจสื่อสารกับซอฟต์แวร์อื่นที่ทำงานบนคอมพิวเตอร์เดียวกันผ่านเซิร์ฟเวอร์ localhost และ 127.0.0.1โดยการทำงานโปรแกรมที่สอดคล้องกับปลั๊กอินเหล่านี้คุณสามารถขยายฟังก์ชันของปลั๊กอินของคุณเกินความสามารถปกติของ Studio เช่นการโต้ตอบกับระบบไฟล์ของคอมพิวเตอร์ของคุณระวังว่าซอฟต์แวร์ดังกล่าวต้องแจกจ่ายแยกต่างหากจากปลั๊กอินเองและอาจเป็นอันตรายต่อความปลอดภัยหากคุณไม่ระวัง

ข้อพิจารณาเพิ่มเติม
  • มีข้อจำกัดด้านพอร์ตคุณไม่สามารถใช้พอร์ต 1194 หรือพอร์ตใดๆ ด้านล่าง 1024 ยกเว้น 80 และ 443หากคุณพยายามใช้พอร์ตที่ถูกบล็อกคุณจะได้รับข้อผิดพลาดเป็น 403 Forbidden หรือ ERR_ACCESS_DENIED
  • สำหรับแต่ละเซิร์ฟเวอร์เกม Roblox มีขีดจํากัดของ 500 คําขอ HTTP ต่อนาทีเกินขีดจำกัดนี้อาจทำให้วิธีการส่งคำขอหยุดทั้งหมดเป็นเวลาประมาณ 30 วินาที
  • ไม่สามารถส่งคำขอไปยังเว็บไซต์ Roblox ใดๆ เช่น www.roblox.com
  • คำขอเว็บสามารถล้มเหลวได้หลายเหตุผลดังนั้นจึงเป็นสิ่งสำคัญที่จะ "โค้ดป้องกัน" (ใช้ pcall()) และมีแผนสำหรับเมื่อคำขอล้มเหลว
  • แม้ว่าโปรโตคอล http:// จะได้รับการสนับสนุน คุณควรใช้ https:// ทุกที่ที่เป็นไปได้
  • คำขอที่ส่งควรให้การยืนยันแบบปลอดภัยเช่นกุญแจลับที่แชร์ล่วงหน้าเพื่อให้ผู้กระทำผิดไม่สามารถแอบอ้างเป็นหนึ่งในเซิร์ฟเวอร์เกม Roblox ของคุณได้
  • ตระหนักถึงความสามารถทั่วไปและนโยบายจํากัดอัตราของเซิร์ฟเวอร์เว็บที่คําขอถูกส่งไปยัง

ตัวอย่างโค้ด

This code sample uses HttpService's GetAsync to make a request to Open Notify, a web service that provides data from NASA. The request is made to an endpoint that provides information on how many astronauts are currently in space. The response is provided in JSON format, so it is parsed using JSONDecode. Finally, the response data is then processed and printed to the Output.

Test this script by pasting the source code into a Script (HttpService cannot be used by LocalScripts). Also, be sure to enable HTTP Requests in your Game Settings (Home > Game Settings).

Astronauts in Space

local HttpService = game:GetService("HttpService")
local URL_ASTROS = "http://api.open-notify.org/astros.json"
-- Make the request to our endpoint URL
local response = HttpService:GetAsync(URL_ASTROS)
-- Parse the JSON response
local data = HttpService:JSONDecode(response)
-- Information in the data table is dependent on the 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.

Where is the International Space Station?

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).

New Pastebin Post

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)

สรุป

คุณสมบัติ

  • การรักษาความปลอดภัยของผู้ใช้ภายใน
    อ่านพร้อมๆ กัน

    บ่งบอกว่าคำขอ HTTP สามารถส่งไปยังเว็บไซต์ภายนอกได้หรือไม่

วิธีการ

  • GenerateGUID(wrapInCurlyBraces : boolean):string
    เขียนพร้อมๆ กัน

    สร้างสตริงสุ่ม UUID/GUID ได้อย่างอิสระโดยมีเครื่องหมายขีดข่วนได้ตามต้องการ

  • เขียนพร้อมๆ กัน

    ส่งคืน Secret จากร้านค้าความลับ

  • JSONDecode(input : string):Variant
    เขียนพร้อมๆ กัน

    ถอดรหัสสตริง JSON เป็นตาราง Luau

  • JSONEncode(input : Variant):string
    เขียนพร้อมๆ กัน

    สร้างสตริง JSON จากตาราง Luau

  • เขียนพร้อมๆ กัน

    แทนที่ตัวอักษรที่ไม่ปลอดภัยด้วยตัวอักษร "%" และตัวอักษรหกตัวทศนิยม

  • GetAsync(url : Variant,nocache : boolean,headers : Variant):string
    ผลตอบแทน

    ส่งคำขอ 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

พารามิเตอร์

wrapInCurlyBraces: boolean

ว่าสตริงที่ส่งคืนควรถูกห่อหุ้มด้วยเครื่องหมายเล็บ ( {} ).

ค่าเริ่มต้น: true

ส่งค่ากลับ

UID ที่สร้างแบบสุ่ม

ตัวอย่างโค้ด

This example uses HttpService's GenerateGUID method to generate and print a universally unique identifier.

HttpService GenerateGUID

local HttpService = game:GetService("HttpService")
local result = HttpService:GenerateGUID(true)
print(result) --> Example output: {4c50eba2-d2ed-4d79-bec1-02a967f49c58}

GetSecret

เขียนพร้อมๆ กัน

วิธีนี้จะคืนค่าที่เพิ่มไปแล้วก่อนหน้านี้ในร้านความลับสำหรับประสบการณ์เนื้อหาลับไม่สามารถพิมพ์ได้และไม่สามารถใช้งานได้เมื่อประสบการณ์ทำงานท้องถิ่น

การคืน Secret ที่ได้รับสามารถแปลงโดยใช้วิธีในตัวที่สร้างขึ้นเช่น Secret:AddPrefix() คาดว่าจะถูกส่งเป็นส่วนหนึ่งของคำขอ HTTP

สำหรับข้อมูลเพิ่มเติม ดูคู่มือการใช้งาน

พารามิเตอร์

key: string
ค่าเริ่มต้น: ""

ส่งค่ากลับ

JSONDecode

Variant
เขียนพร้อมๆ กัน

วิธีนี้เปลี่ยนวัตถุ JSON หรือคอลเลกชันเป็นตาราง Luau ด้วยลักษณะต่อไปนี้:

  • กุญแจของตารางเป็นสตริงหรือตัวเลข แต่ไม่ทั้งสอง หากวัตถุ JSON มีทั้งสอง กุญแจสตริงจะถูกเพิกเฉย
  • วัตถุ JSON ว่างเปล่าสร้างตาราง Luau ว่างเปล่า ( {} )
  • หากสตริง input ไม่ใช่วัตถุ JSON ที่ถูกต้องวิธีนี้จะโยนข้อผิดพลาด

เพื่อเข้ารหัสตาราง Luau เป็นวัตถุ JSON ใช้วิธี HttpService:JSONEncode()

วิธีนี้สามารถใช้ได้ไม่ว่าคำขอ HTTP จะเปิดใช้งานหรือไม่

พารามิเตอร์

input: string

วัตถุ JSON ถูกถอดรหัส

ค่าเริ่มต้น: ""

ส่งค่ากลับ

Variant

วัตถุ 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).

HttpService JSONDecode

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 จะเปิดใช้งานหรือไม่

พารามิเตอร์

input: Variant

ตาราง 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.

HttpService JSONEncode

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

พารามิเตอร์

input: string

สตริง (URL) เพื่อเข้ารหัส

ค่าเริ่มต้น: ""

ส่งค่ากลับ

สตริงที่เข้ารหัส

ตัวอย่างโค้ด

This code sample uses UrlEncode to turn a string into a safe, percent-encoded string that can be used in a URL as an argument. Notice how only unreserved characters (letters, numbers and -_.~) are not transformed into percent-encoded equivalents. Characters with accents are also transformed (for example é is transformed into %C3).

HttpService UrlEncode

local HttpService = game:GetService("HttpService")
local content = "Je suis allé au cinéma." -- French for "I went to the movies"
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).

New Pastebin Post

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 เดียวกัน

พารามิเตอร์

url: Variant

ที่อยู่เว็บที่คุณร้องขอข้อมูล

ค่าเริ่มต้น: ""
nocache: boolean

ว่าคำขอจะเก็บ (แคช) คำตอบหรือไม่

ค่าเริ่มต้น: false
headers: Variant

ใช้เพื่อระบุหัวข้อคำขอ HTTP บางอย่าง

ค่าเริ่มต้น: ""

ส่งค่ากลับ

ร่างคำตอบคำขอ GET

ตัวอย่างโค้ด

This code sample uses HttpService's GetAsync to make a request to Open Notify, a web service that provides data from NASA. The request is made to an endpoint that provides information on how many astronauts are currently in space. The response is provided in JSON format, so it is parsed using JSONDecode. Finally, the response data is then processed and printed to the Output.

Test this script by pasting the source code into a Script (HttpService cannot be used by LocalScripts). Also, be sure to enable HTTP Requests in your Game Settings (Home > Game Settings).

Astronauts in Space

local HttpService = game:GetService("HttpService")
local URL_ASTROS = "http://api.open-notify.org/astros.json"
-- Make the request to our endpoint URL
local response = HttpService:GetAsync(URL_ASTROS)
-- Parse the JSON response
local data = HttpService:JSONDecode(response)
-- Information in the data table is dependent on the 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.

Where is the International Space Station?

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

พารามิเตอร์

url: Variant

ที่อยู่ปลายทางสำหรับข้อมูล

ค่าเริ่มต้น: ""
data: string

ข้อมูลที่ส่ง

ค่าเริ่มต้น: ""
content_type: Enum.HttpContentType

แก้ไขค่าในหัวข้อ Content-Type ที่ส่งไปพร้อมกับคำขอ

ค่าเริ่มต้น: "ApplicationJson"
compress: boolean

กำหนดว่าข้อมูลจะถูกบีบอัด ( บีบอัด ) เมื่อส่ง

ค่าเริ่มต้น: false
headers: Variant

ใช้เพื่อระบุหัวข้อคำขอ 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).

New Pastebin Post

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 คําขอต่อนาทีคำขอเกินเกณฑ์นี้จะล้มเหลวนอกจากนี้ยังมีการยกเว้นโดเมน Robloxซึ่งหมายความว่าคำขอ HTTP ไม่สามารถส่งไปยังเว็บไซต์ที่เป็นเจ้าของของ Roblox เช่น www.roblox.com

พารามิเตอร์

requestOptions: Dictionary

สารานุกรมที่มีข้อมูลที่จะร้องขอจากเซิร์ฟเวอร์ที่ระบุ

ค่าเริ่มต้น: ""

ส่งค่ากลับ

สารานุกรมที่มีข้อมูลการตอบสนองจากเซิร์ฟเวอร์ที่ระบุ

ตัวอย่างโค้ด

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.

Sending an HTTP Request

-- 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

อีเวนต์