KScript 문서
KO

언어

영어에서 자동 번역되었습니다. 원문은 영어입니다.

http_response 필드와 메서드

컨트롤러와 미들웨어가 만든 응답입니다: @response(), @view(), @redirect().

필드는 값에 관한 단순한 정보로, 대괄호 없이 점으로 읽어요: $value.name. 메서드는 인수를 받거나 작업을 수행하며, ->로 호출하거나 $value->name(arguments)처럼 값을 파이프로 넘겨서 호출해요: $value |> name(arguments).

필드

필드타입설명
.bodystring본문을 텍스트로 나타냅니다.
.headersmap헤더를 map으로 나타냅니다.
.is_okbool상태가 2xx이면 true입니다.
.statusnumber200이나 404 같은 HTTP 상태 코드입니다.
.typestringContent-Type입니다.

KScript.Http에서 가져옴

이들은 <import package="KScript.Http" />import KScript.Http가 필요합니다. KScript.Http를 참조하세요.

#http_response->header(name) → string KScript.Http

헤더 하나입니다(없으면 null). Set-Cookie가 여러 번 설정되었다면 첫 번째를 돌려줍니다. 전부 보려면 .headers를 쓰세요.
예시
<let name="response" value="@redirect('/login')" />
<echo>{ $response.status } { $response->header('Location') }</echo>
$response = @redirect('/login')
echo "{ $response.status } { $response->header('Location') }"

#http_response->with_cache(directives) → http_response KScript.Http

Cache-Control을 설정합니다: ->with_cache('public, max-age=300')처럼 쓰거나, 초 단위 숫자로 ->with_cache(300)처럼 씁니다.
예시
<fn name="index" params="request">
  <return value="@view('posts/index')->with_cache(300)" />
</fn>
fn index(request) {
  return @view('posts/index')->with_cache(300)
}

#http_response->with_cookie(name, value, expires?: duration, path?, domain?, http_only?: bool, secure?: bool, same_site?: lax|strict|none) → http_response KScript.Http

쿠키를 설정하고 응답을 반환합니다. 별도로 지정하지 않으면 쿠키는 HttpOnly와 SameSite=Lax로 설정되며, https에서는 Secure가 적용되고, expires:(예: 30d)를 지정하지 않으면 브라우저 세션 동안 유지됩니다.
매개변수받는 값
name 필수모든 값
value required모든 값
expires 선택duration
path 선택모든 값
domain 선택모든 값
http_only 선택플래그: http_only 또는 not http_only
secure 선택플래그: secure 또는 not secure
same_site 선택다음 단어 중 하나: lax|strict|none

인수는 이름을 붙여 지정할 수 있어요: ->with_cookie(…, value: …, expires: …).

예시
<fn name="remember_theme" params="request">
  <return value="@redirect('/')->with_cookie('theme', 'dark', expires: 30d)" />
</fn>
fn remember_theme(request) {
  return @redirect('/')->with_cookie('theme', 'dark', expires: 30d)
}

#http_response->with_etag(value?, weak?: bool) → http_response KScript.Http

ETag를 설정해요: 값의 해시, 또는 본문의 해시(값이 없을 때)예요. etag 미들웨어(또는 etag="true")를 추가하면 If-None-Match에 304로 응답해요.
매개변수받는 값
value 선택모든 값
weak 선택플래그: weak 또는 not weak

인수는 이름을 붙여 지정할 수 있어요: ->with_etag(…, weak: …).

예시
<fn name="show" params="request">
  <return value="@response({ id: 1, title: 'Hello' })->with_etag()" />
</fn>
fn show(request) {
  return @response({ id: 1, title: 'Hello' })->with_etag()
}

#http_response->with_header(name, value) → http_response KScript.Http

헤더를 설정하고 응답을 돌려줘요: $next($request)->with_header('X-Frame-Options', 'DENY'). Set-Cookie는 이전 값을 바꾸지 않고 호출할 때마다 쿠키를 추가해요.

인수는 이름을 붙여 지정할 수 있어요: ->with_header(…, value: …).

예시
<fn name="secure_headers" params="request, next">
  <return value="$next($request)->with_header('X-Frame-Options', 'DENY')" />
</fn>
fn secure_headers(request, next) {
  return $next($request)->with_header('X-Frame-Options', 'DENY')
}

#http_response->with_status(code) → http_response KScript.Http

상태 코드를 바꾸고 응답을 반환합니다.
예시
<fn name="store" params="request">
  <return value="@response({ saved: true })->with_status(201)" />
</fn>
fn store(request) {
  return @response({ saved: true })->with_status(201)
}

#http_response->with_type(type) → http_response KScript.Http

Content-Type을 변경하고 응답을 반환해요.
예시
<fn name="export" params="request">
  <return value="@response('name,email')->with_type('text/csv')" />
</fn>
fn export(request) {
  return @response('name,email')->with_type('text/csv')
}

#http_response->without_cookie(name, path?, domain?) → http_response KScript.Http

브라우저에 쿠키를 삭제하도록 지시하고 응답을 반환해요. 쿠키를 설정할 때 사용한 path와 domain을 지정하세요.

인수는 이름을 붙여 지정할 수 있어요: ->without_cookie(…, path: …, domain: …).

예시
<fn name="forget_theme" params="request">
  <return value="@redirect('/')->without_cookie('theme')" />
</fn>
fn forget_theme(request) {
  return @redirect('/')->without_cookie('theme')
}

any의 메서드