เรียนรู้
Engine Class
HttpService
ไม่สามารถสร้าง
บริการ

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


สรุป
คุณสมบัติ
วิธีการ
GenerateGUID(wrapInCurlyBraces: boolean):string
GetAsync(url: Variant,nocache: boolean,headers: Variant):string
JSONDecode(input: string):Variant
JSONEncode(input: Variant):string
PostAsync(url: Variant,data: string,content_type: Enum.HttpContentType,compress: boolean,headers: Variant):string
สมาชิกที่ได้รับทอด
ตัวอย่างโค้ด
นักบินอวกาศในอวกาศ
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)
-- ข้อมูลในตาราง data ขึ้นอยู่กับการตอบกลับ JSON
if data.message == "success" then
print("ขณะนี้มีนักบินอวกาศ " .. data.number .. " คนในอวกาศ:")
for i, person in pairs(data.people) do
print(i .. ": " .. person.name .. " อยู่ใน " .. person.craft)
end
end
สถานีอวกาศนานาชาติอยู่ที่ไหน?
local HttpService = game:GetService("HttpService")
-- สถานีอวกาศนานาชาติอยู่ที่ไหนตอนนี้?
local URL_ISS = "http://api.open-notify.org/iss-now.json"
local function printISS()
local response
local data
-- ใช้ pcall เพื่อป้องกันในกรณีที่มีอะไรผิดพลาด
pcall(function()
response = HttpService:GetAsync(URL_ISS)
data = HttpService:JSONDecode(response)
end)
-- การร้องขอของเราล้มเหลวหรือไม่หรือ JSON ของเราวิเคราะห์ล้มเหลว?
if not data then
return false
end
-- ตรวจสอบข้อมูลของเราให้ถูกต้องอย่างเต็มที่ ขึ้นอยู่กับว่าคุณจะส่งคำร้องไปที่
-- จุดสิ้นสุดไหน สำหรับตัวอย่างนี้ จุดสิ้นสุดนี้
-- ถูกอธิบายไว้ที่นี่: 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("สถานีอวกาศนานาชาติอยู่ที่:")
print(data.iss_position.latitude .. ", " .. data.iss_position.longitude)
return true
end
end
return false
end
if printISS() then
print("ความสำเร็จ")
else
print("บางอย่างผิดพลาด")
end
โพสต์ใหม่ใน Pastebin
local HttpService = game:GetService("HttpService")
local URL_PASTEBIN_NEW_PASTE = "https://pastebin.com/api/api_post.php"
local dataFields = {
-- คีย์นักพัฒนาของ API ของ Pastebin จาก
-- https://pastebin.com/api#1
["api_dev_key"] = "FILL THIS WITH YOUR API DEVELOPER KEY",
["api_option"] = "paste", -- เก็บเป็น "paste"
["api_paste_name"] = "HttpService:PostAsync", -- ชื่อโพสต์
["api_paste_code"] = "Hello, world", -- เนื้อหาโพสต์
["api_paste_format"] = "text", -- รูปแบบโพสต์
["api_paste_expire_date"] = "10M", -- วันที่หมดอายุ
["api_paste_private"] = "0", -- 0=สาธารณะ, 1=ไม่เปิดเผย, 2=ส่วนตัว
["api_user_key"] = "", -- คีย์ผู้ใช้, หากว่างให้โพสต์ในฐานะแขก
}
-- API ของ pastebin ใช้สตริงที่เข้ารหัสด้วย URL สำหรับข้อมูลโพสต์
-- APIs อื่นอาจใช้ JSON, XML หรือรูปแบบอื่น
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) -- ลบ & ตัวแรกออก
-- นี่คือข้อมูลที่เรากำลังส่ง
print(data)
-- ทำการร้องขอ
local response = HttpService:PostAsync(URL_PASTEBIN_NEW_PASTE, data, Enum.HttpContentType.ApplicationUrlEncoded, false)
-- คำตอบจะเป็น URL สำหรับโพสต์ใหม่ (หรือข้อความผิดพลาดหากมีอะไรผิดพลาด)
print(response)
เปิด Cloud ผ่าน HttpService
-- จำไว้ว่าต้องเปิดใช้งาน HTTP Requests ในการตั้งค่าประสบการณ์!
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}`, -- อัปเดตการเป็นสมาชิกของผู้ใช้ในกลุ่ม
Method = "PATCH",
Headers = {
["Content-Type"] = "application/json", -- เมื่อส่ง JSON ให้ตั้งค่านี้!
["x-api-key"] = HttpService:GetSecret("APIKey"), -- ตั้งค่าใน Creator Hub
},
Body = HttpService:JSONEncode({ role = `groups/{groupId}/roles/{roleId}` }),
})
if response.Success then
print("การตอบกลับสำเร็จ:", response.StatusCode, response.StatusMessage)
else
print("การตอบกลับส่งข้อผิดพลาด:", response.StatusCode, response.StatusMessage)
end
print("เนื้อหาของการตอบกลับ:\n", response.Body)
print("Headers ของการตอบกลับ:\n", HttpService:JSONEncode(response.Headers))
end
-- จำไว้ว่าต้องห่อฟังก์ชันใน 'pcall' เพื่อป้องกันไม่ให้สคริปต์เสียหายในกรณีที่คำขอล้มเหลว
local success, message = pcall(request)
if not success then
print("คำขอ Http ส่งไม่สำเร็จ:", message)
end

เอกสารอ้างอิงเกี่ยวกับ API
คุณสมบัติ
HttpEnabled
การรักษาความปลอดภัยของผู้ใช้ภายใน
อ่านพร้อมๆ กัน
ความสามารถ: Network
HttpService.HttpEnabled:boolean

วิธีการ
CreateWebStreamClient
ความสามารถ: Network
HttpService:CreateWebStreamClient(
streamClientType:Enum.WebStreamClientType, requestOptions:Dictionary
พารามิเตอร์
streamClientType:Enum.WebStreamClientType
requestOptions:Dictionary
ส่งค่ากลับ
ตัวอย่างโค้ด
HttpService สร้างลูกค้าเว็บสตรีม SSE
local HttpService = game:GetService("HttpService")
local URL_GEMINI_SSE = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:streamGenerateContent?alt=sse&key="
local geminiSecret = HttpService:GetSecret("gemini_secret") -- สมมติว่าคุณได้อัปโหลดคีย์ API ของ Gemini เป็นความลับในเครื่องแล้ว
local url_with_secret = geminiSecret:AddPrefix(URL_GEMINI_SSE) -- จำไว้ว่าปลั๊กอินไม่สามารถเข้าถึงความลับในเครื่องได้!
local function handleOpen(responseStatusCode, headers)
print("สตรีมเปิดด้วยรหัสการตอบกลับ:", responseStatusCode)
end
local function handleMessage(message)
-- แยกพ้อยท์ตามประเภทของสตรีม
print("สตรีมข้อความได้รับ:", message)
end
local function request()
local sse_client = HttpService:CreateWebStreamClient(Enum.WebStreamClientType.RawStream, {
Url = url_with_secret,
Method = "POST",
Headers = {
["Content-Type"] = "application/json", -- เมื่อส่ง JSON ให้ตั้งค่านี้!
},
Body = HttpService:JSONEncode({ contents = { parts = { text ="โปรโตคอล SSE ทำงานอย่างไร?" }}}),
})
local openConnection = sse_client.Opened:Connect(handleOpen)
local messageConnection = sse_client.MessageReceived:Connect(handleMessage)
sse_client.Closed:Wait()
openConnection:Disconnect()
messageConnection:Disconnect()
end
-- จำไว้ว่าต้องหุ้มฟังก์ชันใน 'pcall' เพื่อป้องกันไม่ให้สคริปต์เกิดข้อผิดพลาดหากคำขอล้มเหลว
local success, message = pcall(request)
if not success then
print("WebStreamClient ล้มเหลว:", message)
end
HttpService สร้างเว็บสตรีมคลังลูกค้า RawStream
local HttpService = game:GetService("HttpService")
local LOCAL_LLM_PORT = "http://localhost:11434/api/generate?stream=true"
local function handleMessage(message)
-- แยกเหตุการณ์ตามประเภทสตรีม
-- ถ้าหากว่าส่วนหัว Content-Type ของการตอบกลับเป็น JSON, คุณสามารถถอดรหัสได้แบบนี้:
local json = HttpService:JSONDecode(message)
end
local function handleError(responseStatusCode, errorMessage)
print("เกิดข้อผิดพลาดในสตรีมด้วยรหัสการตอบกลับ:", responseStatusCode)
end
local function request()
local sse_client = HttpService:CreateWebStreamClient(Enum.WebStreamClientType.RawStream, {
-- เซิร์ฟเวอร์ Llama3.2 LLM ทำงานในเครื่องโดยใช้ Ollama
Url = LOCAL_LLM_PORT,
Method = "POST",
Headers = {
["Content-Type"] = "application/json", -- เมื่อส่งข้อมูล JSON, ตั้งค่านี้ด้วย!
},
Body = HttpService:JSONEncode({ model="llama3.2", prompt ="บอกมาหน่อยว่าอะไรคือมุขตลกที่ตลก"}),
})
local messageConnection = sse_client.MessageReceived:Connect(handleMessage)
local errorConnection = sse_client.Error:Connect(handleError)
sse_client.Closed:Wait()
messageConnection:Disconnect()
errorConnection:Disconnect()
sse_client:close()
end
-- จำไว้ว่าต้องห่อหุ้มฟังก์ชันใน 'pcall' เพื่อป้องกันไม่ให้สคริปต์ล้มเหลวถ้าหากคำขอล้มเหลว
local success, message = pcall(request)
if not success then
print("WebStreamClient ล้มเหลว:", message)
end

GenerateGUID
เขียนพร้อมๆ กัน
HttpService:GenerateGUID(wrapInCurlyBraces:boolean):string
พารามิเตอร์
wrapInCurlyBraces:boolean
ค่าเริ่มต้น: true
ส่งค่ากลับ
ตัวอย่างโค้ด
HttpService สร้าง GUID
local HttpService = game:GetService("HttpService")
local result = HttpService:GenerateGUID(true)
print(result) --> ผลลัพธ์ตัวอย่าง: {4c50eba2-d2ed-4d79-bec1-02a967f49c58}

GetAsync
ผลตอบแทน
ความสามารถ: Network
HttpService:GetAsync(
url:Variant, nocache:boolean, headers:Variant
พารามิเตอร์
url:Variant
nocache:boolean
ค่าเริ่มต้น: false
headers:Variant
ส่งค่ากลับ
ตัวอย่างโค้ด
นักบินอวกาศในอวกาศ
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)
-- ข้อมูลในตาราง data ขึ้นอยู่กับการตอบกลับ JSON
if data.message == "success" then
print("ขณะนี้มีนักบินอวกาศ " .. data.number .. " คนในอวกาศ:")
for i, person in pairs(data.people) do
print(i .. ": " .. person.name .. " อยู่ใน " .. person.craft)
end
end
สถานีอวกาศนานาชาติอยู่ที่ไหน?
local HttpService = game:GetService("HttpService")
-- สถานีอวกาศนานาชาติอยู่ที่ไหนตอนนี้?
local URL_ISS = "http://api.open-notify.org/iss-now.json"
local function printISS()
local response
local data
-- ใช้ pcall เพื่อป้องกันในกรณีที่มีอะไรผิดพลาด
pcall(function()
response = HttpService:GetAsync(URL_ISS)
data = HttpService:JSONDecode(response)
end)
-- การร้องขอของเราล้มเหลวหรือไม่หรือ JSON ของเราวิเคราะห์ล้มเหลว?
if not data then
return false
end
-- ตรวจสอบข้อมูลของเราให้ถูกต้องอย่างเต็มที่ ขึ้นอยู่กับว่าคุณจะส่งคำร้องไปที่
-- จุดสิ้นสุดไหน สำหรับตัวอย่างนี้ จุดสิ้นสุดนี้
-- ถูกอธิบายไว้ที่นี่: 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("สถานีอวกาศนานาชาติอยู่ที่:")
print(data.iss_position.latitude .. ", " .. data.iss_position.longitude)
return true
end
end
return false
end
if printISS() then
print("ความสำเร็จ")
else
print("บางอย่างผิดพลาด")
end

GetSecret
เขียนพร้อมๆ กัน
ความสามารถ: Network
HttpService:GetSecret(key:string):Secret
พารามิเตอร์
key:string
ส่งค่ากลับ

JSONDecode
เขียนพร้อมๆ กัน
HttpService:JSONDecode(input:string):Variant
พารามิเตอร์
input:string
ส่งค่ากลับ
Variant
ตัวอย่างโค้ด
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
-- เนื่องจาก tab["hello"] และ tab.hello เท่ากัน,
-- คุณสามารถใช้ data["info"]["points"] ที่นี่ได้เช่นกัน:
print("ฉันมี " .. data.info.points .. " คะแนน")
if data.info.isLeader then
print("ฉันเป็นผู้นำ")
end
print("ฉันมี " .. #data.info.past_scores .. " คะแนนที่ผ่านมา")
print("ข้อมูลทั้งหมด:")
for key, value in pairs(data.info) do
print(key, typeof(value), value)
end
end

JSONEncode
เขียนพร้อมๆ กัน
HttpService:JSONEncode(input:Variant):string
พารามิเตอร์
input:Variant
ส่งค่ากลับ
ตัวอย่างโค้ด
HttpService JSONEncode
local HttpService = game:GetService("HttpService")
local tab = {
-- จำไว้: บรรทัดเหล่านี้เท่ากัน
--["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)

PostAsync
ผลตอบแทน
ความสามารถ: Network
HttpService:PostAsync(
url:Variant, data:string, content_type:Enum.HttpContentType, compress:boolean, headers:Variant
พารามิเตอร์
url:Variant
data:string
ค่าเริ่มต้น: "ApplicationJson"
compress:boolean
ค่าเริ่มต้น: false
headers:Variant
ส่งค่ากลับ
ตัวอย่างโค้ด
โพสต์ใหม่ใน Pastebin
local HttpService = game:GetService("HttpService")
local URL_PASTEBIN_NEW_PASTE = "https://pastebin.com/api/api_post.php"
local dataFields = {
-- คีย์นักพัฒนาของ API ของ Pastebin จาก
-- https://pastebin.com/api#1
["api_dev_key"] = "FILL THIS WITH YOUR API DEVELOPER KEY",
["api_option"] = "paste", -- เก็บเป็น "paste"
["api_paste_name"] = "HttpService:PostAsync", -- ชื่อโพสต์
["api_paste_code"] = "Hello, world", -- เนื้อหาโพสต์
["api_paste_format"] = "text", -- รูปแบบโพสต์
["api_paste_expire_date"] = "10M", -- วันที่หมดอายุ
["api_paste_private"] = "0", -- 0=สาธารณะ, 1=ไม่เปิดเผย, 2=ส่วนตัว
["api_user_key"] = "", -- คีย์ผู้ใช้, หากว่างให้โพสต์ในฐานะแขก
}
-- API ของ pastebin ใช้สตริงที่เข้ารหัสด้วย URL สำหรับข้อมูลโพสต์
-- APIs อื่นอาจใช้ JSON, XML หรือรูปแบบอื่น
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) -- ลบ & ตัวแรกออก
-- นี่คือข้อมูลที่เรากำลังส่ง
print(data)
-- ทำการร้องขอ
local response = HttpService:PostAsync(URL_PASTEBIN_NEW_PASTE, data, Enum.HttpContentType.ApplicationUrlEncoded, false)
-- คำตอบจะเป็น URL สำหรับโพสต์ใหม่ (หรือข้อความผิดพลาดหากมีอะไรผิดพลาด)
print(response)

RequestAsync
ผลตอบแทน
ความสามารถ: Network
HttpService:RequestAsync(requestOptions:Dictionary):Dictionary
พารามิเตอร์
requestOptions:Dictionary
ส่งค่ากลับ
ตัวอย่างโค้ด
การส่งคำขอ HTTP
-- จำไว้ว่าต้องเปิดใช้งาน HTTP Requests ในการตั้งค่าประสบการณ์!
local HttpService = game:GetService("HttpService")
local function request()
local response = HttpService:RequestAsync({
Url = "http://httpbin.org/post", -- เว็บไซต์นี้ช่วยในการแก้ไขปัญหา HTTP requests
Method = "POST",
Headers = {
["Content-Type"] = "application/json", -- เมื่อต้องการส่ง JSON ต้องตั้งค่านี้!
},
Body = HttpService:JSONEncode({ hello = "world" }),
})
if response.Success then
print("รหัสสถานะ:", response.StatusCode, response.StatusMessage)
print("เนื้อหาการตอบกลับ:\n", response.Body)
else
print("คำขอล้มเหลว:", response.StatusCode, response.StatusMessage)
end
end
-- จำไว้ว่าต้องห่อหุ้มฟังก์ชันด้วย 'pcall' เพื่อป้องกันไม่ให้สคริปต์ขัดข้องหากคำขอล้มเหลว
local success, message = pcall(request)
if not success then
print("Http Request ล้มเหลว:", message)
end
เปิด Cloud ผ่าน HttpService
-- จำไว้ว่าต้องเปิดใช้งาน HTTP Requests ในการตั้งค่าประสบการณ์!
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}`, -- อัปเดตการเป็นสมาชิกของผู้ใช้ในกลุ่ม
Method = "PATCH",
Headers = {
["Content-Type"] = "application/json", -- เมื่อส่ง JSON ให้ตั้งค่านี้!
["x-api-key"] = HttpService:GetSecret("APIKey"), -- ตั้งค่าใน Creator Hub
},
Body = HttpService:JSONEncode({ role = `groups/{groupId}/roles/{roleId}` }),
})
if response.Success then
print("การตอบกลับสำเร็จ:", response.StatusCode, response.StatusMessage)
else
print("การตอบกลับส่งข้อผิดพลาด:", response.StatusCode, response.StatusMessage)
end
print("เนื้อหาของการตอบกลับ:\n", response.Body)
print("Headers ของการตอบกลับ:\n", HttpService:JSONEncode(response.Headers))
end
-- จำไว้ว่าต้องห่อฟังก์ชันใน 'pcall' เพื่อป้องกันไม่ให้สคริปต์เสียหายในกรณีที่คำขอล้มเหลว
local success, message = pcall(request)
if not success then
print("คำขอ Http ส่งไม่สำเร็จ:", message)
end

UrlEncode
เขียนพร้อมๆ กัน
ความสามารถ: Network
HttpService:UrlEncode(input:string):string
พารามิเตอร์
input:string
ส่งค่ากลับ
ตัวอย่างโค้ด
รหัสบริการ HttpService UrlEncode
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
local HttpService = game:GetService("HttpService")
local URL_PASTEBIN_NEW_PASTE = "https://pastebin.com/api/api_post.php"
local dataFields = {
-- คีย์นักพัฒนาของ API ของ Pastebin จาก
-- https://pastebin.com/api#1
["api_dev_key"] = "FILL THIS WITH YOUR API DEVELOPER KEY",
["api_option"] = "paste", -- เก็บเป็น "paste"
["api_paste_name"] = "HttpService:PostAsync", -- ชื่อโพสต์
["api_paste_code"] = "Hello, world", -- เนื้อหาโพสต์
["api_paste_format"] = "text", -- รูปแบบโพสต์
["api_paste_expire_date"] = "10M", -- วันที่หมดอายุ
["api_paste_private"] = "0", -- 0=สาธารณะ, 1=ไม่เปิดเผย, 2=ส่วนตัว
["api_user_key"] = "", -- คีย์ผู้ใช้, หากว่างให้โพสต์ในฐานะแขก
}
-- API ของ pastebin ใช้สตริงที่เข้ารหัสด้วย URL สำหรับข้อมูลโพสต์
-- APIs อื่นอาจใช้ JSON, XML หรือรูปแบบอื่น
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) -- ลบ & ตัวแรกออก
-- นี่คือข้อมูลที่เรากำลังส่ง
print(data)
-- ทำการร้องขอ
local response = HttpService:PostAsync(URL_PASTEBIN_NEW_PASTE, data, Enum.HttpContentType.ApplicationUrlEncoded, false)
-- คำตอบจะเป็น URL สำหรับโพสต์ใหม่ (หรือข้อความผิดพลาดหากมีอะไรผิดพลาด)
print(response)

©2026 Roblox Corporation. Roblox, โลโก้ Roblox และ Powering Imagination เป็นส่วนหนึ่งของเครื่องหมายการค้าที่จดทะเบียน และไม่ได้จดทะเบียนของเราในสหรัฐฯ และประเทศอื่นๆ