Skip to main content

sendJson()

함수 시그니처

sendJson: (data?: unknown) => DenchCreateBuilder<T>

관련 RequestInit 및 Header 속성

{
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}

설명

요청 데이터를 JSON 문자열로 직렬화하고 Content-Type: application/json 헤더를 설정합니다.

data를 전달하면 해당 값이 요청 body가 됩니다. 생략하면 현재 빌더의 config.options.body에 저장된 값을 JSON으로 직렬화합니다.

예제

import { dench } from 'dench-fetch';

const response = await dench('https://api.example.com')
.post('/users')
.sendJson({ name: 'Dench', active: true })
.toResponse();

실제 RequestInit과 Header는 다음과 같습니다.

{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: '{"name":"Dench","active":true}',
}

주의사항

  • 현재 실제 구현은 post(api, data)put(api, data)의 두 번째 인자를 저장하지 않습니다. 요청 데이터는 sendJson(data)에 직접 전달하세요.
  • data와 기존 body가 모두 없다면 body는 undefined가 되지만 JSON Content-Type 헤더는 설정됩니다.
  • 이미 설정된 body와 Content-Type 헤더를 덮어씁니다.
  • 인자 없이 반복 적용하면 이미 직렬화된 문자열이 다시 JSON 문자열로 직렬화될 수 있습니다.