Request():setHeader()
HTTP 요청에 새 헤더를 설정합니다. 이 메서드를 사용하면 요청을 보내기 전에 사용자 지정 HTTP 헤더를 추가할 수 있습니다.
시그니처
setHeader(key: string, value: string): Request
매개변수
| Parameters | Type | Description |
|---|---|---|
| key | string | 헤더 이름 |
| value | string | 헤더 값 |
반환값
Request - 메서드 체이닝을 위해 Request 객체를 반환합니다.
설명
setHeader() 메서드는 요청에 사용자 지정 HTTP 헤더를 추가합니다. 헤더는 콘텐츠 타입, 인증, 사용자 에이전트 등 요청에 대한 추가 정보를 제공하는 메타데이터입니다. 이 메서드를 여러 번 호출하여 여러 헤더를 추가할 수 있습니다.
예시
인증 헤더 설정
-- API 인증을 위한 Bearer 토큰 추가
local response = Request("https://api.example.com/data")
:setHeader("Authorization", "Bearer your-token-here")
:get()
콘텐츠 타입 설정
-- POST 요청을 위한 JSON 콘텐츠 타입 설정
local response = Request("https://api.example.com/users")
:setHeader("Content-Type", "application/json")
:setParameter("name", "John Doe")
:setParameter("email", "john@example.com")
:post()
여러 헤더 설정
-- 요청에 여러 헤더 설정
local response = Request("https://api.example.com/protected")
:setHeader("Authorization", "Bearer token123")
:setHeader("Content-Type", "application/json")
:setHeader("Accept", "application/json")
:setHeader("User-Agent", "MyApp/1.0")
:get()
사용자 지정 헤더
-- 애플리케이션별 사용자 지정 헤더 추가
local response = Request("https://api.example.com/process")
:setHeader("X-API-Key", "your-api-key")
:setHeader("X-Request-ID", "unique-request-identifier")
:setHeader("X-Client-Version", "2.1.0")
:post()
폼 데이터 헤더
-- 폼 데이터 제출을 위한 헤더 설정
local response = Request("https://example.com/login")
:setHeader("Content-Type", "application/x-www-form-urlencoded")
:setParameter("username", "user")
:setParameter("password", "pass")
:post()
일반적인 HTTP 헤더
| Header Name | Typical Usage | Example |
|---|---|---|
Authorization | 인증 토큰 | "Bearer token123" |
Content-Type | 요청 본문 형식 | "application/json" |
Accept | 응답 형식 기본 설정 | "application/json" |
User-Agent | 클라이언트 식별 | "MyApp/1.0" |
X-API-Key | API 인증 | "your-api-key" |
Content-Length | 요청 본문 크기(일반적으로 자동 설정) | "256" |
참고
- 헤더는 대소문자를 구분하지 않지만, Header-Case(예:
Content-Type)를 사용하는 것이 관례입니다. - 이 메서드는 Request 객체를 반환하여 메서드 체이닝을 허용합니다.
- 일부 헤더는 HTTP 클라이언트에 의해 자동으로 설정될 수 있습니다.
Content-Length와 같은 특정 헤더는 일반적으로 자동으로 계산됩니다.- 인증 헤더에 주의하고 민감한 정보를 노출하지 마세요.
- 동일한 헤더 이름을 여러 번 설정하여 헤더를 재정의할 수 있습니다.