Response
Objek Response berisi hasil dari permintaan HTTP yang dibuat dengan modul Request. Ini menyediakan akses ke data respons, kode status, dan metadata lainnya yang dikembalikan oleh server.
Ikhtisar
Objek Response dikembalikan oleh metode permintaan HTTP seperti get(), post(), put(), patch(), dan delete(). Objek ini merangkum respons server dan menyediakan metode untuk mengakses konten dan metadata respons.
Metode
getData(): string
Mendapatkan konten body respons sebagai string.
Mengembalikan
string - Konten body respons
getResponseCode(): number
Mendapatkan kode status HTTP dari respons.
Mengembalikan
number - Kode status HTTP (misalnya, 200, 404, 500)
Pola Penggunaan Umum
-- Make a request and handle the response
local response = Request("https://api.example.com/data"):get()
-- Get the status code to check if the request was successful
local statusCode = response:getResponseCode()
if statusCode == 200 then
-- Request was successful, get the data
local data = response:getData()
print("Response data: " .. data)
else
-- Request failed, handle the error
print("Request failed with status: " .. statusCode)
local errorData = response:getData()
if errorData and errorData ~= "" then
print("Error details: " .. errorData)
end
end
Kategori Kode Status HTTP
| Status Range | Meaning | Typical Response |
|---|---|---|
| 200-299 | Sukses | Berisi data yang diminta atau konfirmasi |
| 300-399 | Pengalihan | Biasanya kosong atau berisi info pengalihan |
| 400-499 | Error Klien | Berisi deskripsi error |
| 500-599 | Error Server | Berisi deskripsi error |
Format Data Respons
Data respons selalu dikembalikan sebagai string, terlepas dari format aslinya:
- JSON:
"{\"key\":\"value\"}"- Mungkin perlu parsing jika Anda memiliki parser JSON - XML:
"<?xml version=\"1.0\"?><root>...</root>"- Konten XML sebagai string - Teks Biasa:
"Simple text response"- Konten teks langsung - HTML:
"<html><body>...</body></html>"- Konten HTML sebagai string - Kosong:
""- Tidak ada konten (umum dengan respons 204)
Praktik Terbaik
- Selalu periksa kode status terlebih dahulu sebelum memproses data respons
- Tangani respons kosong dengan baik, terutama dengan kode status 204
- Periksa konten respons bahkan untuk respons error karena mungkin berisi informasi yang berguna
- Bersiaplah untuk error jaringan yang dapat mencegah objek respons dibuat
Contoh
Penanganan Respons Dasar
local response = Request("https://api.example.com/users/123"):get()
local status = response:getResponseCode()
if status == 200 then
local userData = response:getData()
print("User data: " .. userData)
else
print("Failed to get user data. Status: " .. status)
end
Penanganan Error
local response = Request("https://api.example.com/invalid"):get()
local status = response:getResponseCode()
if status == 404 then
print("Resource not found")
elseif status == 401 then
print("Authentication required")
elseif status >= 500 then
print("Server error: " .. status)
local errorDetails = response:getData()
print("Error details: " .. errorDetails)
else
print("Unexpected status code: " .. status)
end
Bekerja dengan Metode HTTP Berbeda
-- GET request
local getResponse = Request("https://api.example.com/data"):get()
print("GET status: " .. getResponse:getResponseCode())
-- POST request
local postResponse = Request("https://api.example.com/data")
:setParameter("key", "value")
:post()
print("POST status: " .. postResponse:getResponseCode())
-- DELETE request
local deleteResponse = Request("https://api.example.com/data/123"):delete()
print("DELETE status: " .. deleteResponse:getResponseCode())