본문으로 건너뛰기

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 작업은 일반적으로 다음을 반환합니다.
    • 200 OK - 응답 본문에 업데이트된 리소스 포함(업데이트 시)
    • 201 Created - 응답 본문에 새 리소스 포함(생성 시)
    • 204 No Content - 작업이 성공했지만 응답 본문이 필요 없는 경우
  • 이 메서드는 응답 데이터와 상태 코드에 접근할 수 있는 Response 객체를 반환합니다.
  • 요청이 성공했는지 확인하려면 항상 응답 상태 코드를 확인하세요.
  • PUT 요청은 멱등성을 가집니다. 즉, 동일한 요청을 여러 번 보내도 동일한 효과를 냅니다.
  • PUT 요청은 기존 데이터를 완전히 교체하므로 사용에 주의하세요.