서비스 응답 모듈
서비스 응답 모듈 (kong.service.response)
이 문서는 Kong PDK의 kong.service.response 모듈을 설명해요. 이 모듈은 업스트림 Service가 돌려준 응답을 다루는 함수들을 모아 놓은 곳이에요. get_status로 HTTP 상태 코드를 확인하고, get_headers나 get_header로 헤더를 읽고, get_body나 set_body로 응답 본문을 가져오거나 교체할 수 있어요. kong.response가 Kong 자체가 만든 응답을 다룬다면, 이 모듈은 오직 Service가 보낸 응답만 다룬다는 점이 핵심 차이예요
출처: 문서
본문
kong.service.response.get_status()
Service에서 온 응답의 HTTP 상태 코드를 Lua 숫자로 반환해요.
페이즈
header_filter,body_filter,log
반환값
number|nil: Service에서 온 응답의 상태 코드예요. 요청이 프록시되지 않았다면(즉,kong.response.get_source()가"service"이외의 값을 반환한 경우)nil이 반환돼요.
사용 예제
kong.log.inspect(kong.service.response.get_status()) -- 418
kong.service.response.get_headers([max_headers])
Service 응답의 헤더를 담고 있는 Lua 테이블을 반환해요. 키는 헤더 이름이고, 값은 헤더 값이 담긴 문자열이거나 헤더가 여러 번 전송된 경우 문자열 배열이에요. 이 테이블에서 헤더 이름은 대소문자를 구분하지 않고, 대시(-)는 언더스코어(_)로 쓸 수도 있어요. 즉, X-Custom-Header 헤더는 x_custom_header로도 가져올 수 있어요.
kong.response.get_headers()와 달리, 이 함수는 Service 응답에 존재하는 헤더만 반환해요(Kong 자체가 추가한 헤더는 무시해요). 요청이 Service로 프록시되지 않았다면(예: 인증 플러그인이 요청을 거부하고 HTTP 401 응답을 만든 경우) Service에서 온 응답을 받지 못한 것이므로 반환되는 headers 값이 nil일 수 있어요.
기본적으로 이 함수는 최대 100개까지의 헤더를 반환해요. 선택적인 max_headers 인자를 지정해서 이 한도를 조정할 수 있지만, 1보다 커야 하고 1000보다 커서는 안 돼요.
페이즈
header_filter,body_filter,log
파라미터
- max_headers (
number, optional): 파싱할 수 있는 헤더의 최대 개수에 대한 한도를 설정해요.
반환값
-
table: 테이블 형태의 응답 헤더예요. -
string:max_headers보다 많은 헤더가 존재하면 에러"truncated"가 담긴 문자열을 반환해요.
사용 예제
-- Given a response with the following headers:
-- X-Custom-Header: bla
-- X-Another: foo bar
-- X-Another: baz
local headers = kong.service.response.get_headers()
if headers then
kong.log.inspect(headers.x_custom_header) -- "bla"
kong.log.inspect(headers.x_another[1]) -- "foo bar"
kong.log.inspect(headers["X-Another"][2]) -- "baz"
end
Note that this function returns a proxy table
which cannot be iterated with `pairs` or used as operand of `#`.
kong.service.response.get_header(name)
지정한 응답 헤더의 값을 반환해요.
kong.response.get_header()와 달리, 이 함수는 Service 응답에 존재하는 헤더만 반환해요(Kong 자체가 추가한 헤더는 무시해요).
페이즈
header_filter,body_filter,log
파라미터
-
name (
string): 헤더의 이름이에요.헤더 이름은 대소문자를 구분하지 않고 소문자로 정규화돼요. 대시(
-)는 언더스코어(_)로 쓸 수도 있어요. 즉,X-Custom-Header헤더는x_custom_header로도 가져올 수 있어요.
반환값
string|nil: 헤더의 값이에요. 응답에서name에 해당하는 헤더를 찾을 수 없으면nil이 반환돼요. 같은 이름의 헤더가 응답에 여러 번 존재하면 이 함수는 첫 번째로 나타난 헤더의 값을 반환해요.
사용 예제
-- Given a response with the following headers:
-- X-Custom-Header: bla
-- X-Another: foo bar
-- X-Another: baz
kong.log.inspect(kong.service.response.get_header("x-custom-header")) -- "bla"
kong.log.inspect(kong.service.response.get_header("X-Another")) -- "foo bar"
kong.service.response.get_raw_body()
버퍼링된 원본 바디를 반환해요.
페이즈
header_filter,body_filter,log
반환값
string: 버퍼링된 원본 바디예요.
사용 예제
-- Plugin needs to call kong.service.request.enable_buffering() on `rewrite`
-- or `access` phase prior calling this function.
local body = kong.service.response.get_raw_body()
kong.service.response.get_body([mimetype[, max_args[, decompressed]]])
디코딩된 버퍼링 바디를 반환해요.
페이즈
header_filter,body_filter,log
파라미터
- mimetype (
string, optional): 응답의 MIME 타입이에요(알려진 경우). - max_args (
number, optional): (무엇?)의 최대 개수에 대한 한도를 설정해요 - decompressed (
boolean, optional): 압축되어 있는 경우 파싱할 수 있는 압축 해제된 바디를 가져와요.
반환값
-
table|nil: 디코딩된 버퍼링 바디예요. -
string|nil: 에러 메시지예요. -
string|nil: mimetype 사용된 MIME 타입이에요.
사용 예제
-- Plugin needs to call kong.service.request.enable_buffering() on `rewrite`
-- or `access` phase prior calling this function.
local body = kong.service.response.get_body()
kong.service.response.set_body(body)
버퍼링된 응답의 바디를 설정해요.
이 함수는 바디 길이에 맞게 Content-Length 헤더를 변경하고 Content-Encoding 헤더를 지워요.
요청이 버퍼링되고 있지 않거나 바디가 문자열이 아니면 에러가 발생해요.
페이즈
response
파라미터
- body (
string): 바디예요.
반환값
- 없음. 잘못된 입력이면 에러를 던져요.
사용 예제
local body = kong.service.response.get_body()
if body then
body = transform(body)
kong.service.response.set_body(body)
end
관련 리소스
더 알아보기 (Learn more)
- Kong 플러그인 개발 키트 전체를 한눈에 훑어보고 싶을 때 출발점으로 좋아요 — PDK
- Kong이 직접 생성하는 응답을 다루는 함수들과의 차이를 비교해 보면 이해가 쉬워요 — kong.response
- Service 관련 나머지 PDK 함수들이 궁금하면 이 문서를 참고해요 — kong.service
- Service로 보내는 요청을 수정하는 방법이 필요하면 이 모듈 문서를 보세요 — kong.service.request