メインコンテンツまでスキップ

Request():put()

PUT リクエストを実行し、Response オブジェクトを返します。このメソッドは、サーバー上のリソースを完全に置き換えるために使用します。

シグネチャ

put(): Response

戻り値

Response - サーバーのレスポンスを含むレスポンスオブジェクト

説明

put() メソッドは、Request オブジェクトの作成時に指定した URL に対して HTTP PUT リクエストを送信します。PUT リクエストは通常、既存のリソースを新しいデータで完全に置き換えるため、または特定の URL にリソースを作成するために使用します。

put() を呼び出す前に setParameter() を使用してリクエストボディにデータを追加します。

基本的な PUT リクエスト

-- ユーザーの完全なデータを置き換え
local response = Request("https://api.example.com/users/123")
:setParameter("name", "John Smith")
:setParameter("email", "john.smith@example.com")
:setParameter("age", "35")
:setParameter("status", "active")
:put()

JSON ヘッダー付き PUT リクエスト

-- 投稿全体を新しいコンテンツで置き換え
local response = Request("https://api.example.com/posts/456")
:setHeader("Content-Type", "application/json")
:setHeader("Authorization", "Bearer your-token-here")
:setParameter("title", "Completely New Title")
:setParameter("body", "Completely new post content")
:setParameter("userId", "1")
:setParameter("status", "published")
:put()

PUT レスポンスの処理

-- ユーザープロフィールを更新してレスポンスを処理
local response = Request("https://api.example.com/users/789")
:setHeader("Content-Type", "application/json")
:setHeader("Authorization", "Bearer token123")
:setParameter("name", "Jane Doe")
:setParameter("email", "jane.doe@example.com")
:setParameter("bio", "Software Developer")
:setParameter("website", "https://jane.example.com")
:setParameter("location", "San Francisco")
:put()

local data = response:getData()
local statusCode = response:getResponseCode()

if statusCode == 200 then
print("ユーザーを更新しました!")
print("更新されたユーザーデータ: " .. data)
elseif statusCode == 201 then
print("ユーザーを作成しました!")
print("新しいユーザーデータ: " .. data)
else
print("ユーザーの更新に失敗しました。ステータス: " .. statusCode)
end

リソース作成のための PUT

-- 特定の URL に新しいリソースを作成
local response = Request("https://api.example.com/files/document-v2")
:setHeader("Content-Type", "application/json")
:setParameter("content", "This is the new document content")
:setParameter("version", "2.0")
:setParameter("author", "John Doe")
:put()

local status = response:getResponseCode()
if status == 201 then
print("ドキュメントを作成しました!")
elseif status == 200 then
print("ドキュメントを置き換えました!")
else
print("操作に失敗しました。ステータス: " .. status)
end

条件付き PUT 更新

-- リソースが変更されていない場合のみ更新
local response = Request("https://api.example.com/articles/123")
:setHeader("Content-Type", "application/json")
:setHeader("If-Match", "etag-value-here")
:setHeader("Authorization", "Bearer token")
:setParameter("title", "Updated Article Title")
:setParameter("content", "Updated article content")
:setParameter("author", "John Doe")
:put()

local status = response:getResponseCode()
if status == 412 then
print("更新に失敗しました: 別のユーザーがリソースを変更しています")
elseif status == 200 or status == 201 then
print("記事の更新/置き換えに成功しました")
end

リソースの完全な置き換え

-- 商品全体を新しいデータで置き換え
local response = Request("https://api.example.com/products/456")
:setHeader("Content-Type", "application/json")
:setParameter("name", "Premium Widget")
:setParameter("description", "A high-quality widget with advanced features")
:setParameter("price", "99.99")
:setParameter("stock", "50")
:setParameter("category", "electronics")
:setParameter("specs", '{"color": "black", "weight": "200g", "warranty": "2 years"}')
:put()

local updatedProduct = response:getData()
print("商品を置き換えました: " .. updatedProduct)

注記

  • put() を呼び出す前に setParameter() を使用してリクエストボディにデータを追加します
  • PUT リクエストには変更差分ではなく、リソースの完全な表現を含める必要があります
  • PUT と PATCH の違いは、PUT はリソース全体を置き換えるのに対し、PATCH は部分的な更新を行う点です
  • 成功した PUT 操作は通常、次のいずれかを返します:
    • 200 OK — レスポンスボディに更新されたリソースを含む(更新時)
    • 201 Created — レスポンスボディに新しいリソースを含む(作成時)
    • 204 No Content — 操作は成功したがレスポンスボディが不要な場合
  • このメソッドは Response オブジェクトを返し、レスポンスデータとステータスコードにアクセスできます
  • リクエストが成功したかどうかを確認するため、常にレスポンスのステータスコードをチェックしてください
  • PUT リクエストは冪等です — 同じリクエストを複数回送信しても同じ結果になります
  • PUT リクエストは既存のデータを完全に置き換えるため、使用には注意してください