Skip to content

fetchData

Node.js 런타임 필요 ('qsu/node')

이 함수는 node:fetch를 조금 더 쉽게 사용하면서도 응답 상태에 상관 없이 데이터를 반환받기 위해 사용됩니다. 기본적으로 GET method를 사용하지만 별도 옵션으로 POST 등의 method도 사용할 수 있습니다.

요청 URL의 Content-Type에 따라 반환하는 데이터 형식이 string이거나 object일 수 있습니다.

응답에 실패했거나 204 (No Content) 응답 코드일 때, 에러가 발생했을 때에도 별도의 exception 없이 null을 반환합니다.

다만 onError 이벤트를 통해 에러를 받아볼 수 있습니다.

Parameters

이름타입필수기본값
urlstring
httpRequestOptionsHTTPRequestOption
요청 옵션입니다. 아래 표를 참고하세요.
HTTPRequestOption
이름타입필수기본값
authobject
인증 정보입니다. 아래 표를 참고하세요.
getboolean
Same as method: 'get'
postboolean
Same as method: 'post'
putboolean
Same as method: 'put'
deleteboolean
Same as method: 'delete'
patchboolean
Same as method: 'patch'
toStreamboolean
timeoutnumber
method'get' | 'post' | 'put' | 'delete' | 'patch'
hoststring
If this value is not specified, the URL must be a full path.
queryParametersobject
bodyobject | string | FormData | null
bodyType'text' | 'json' | 'form-data' | 'x-www-form-urlencoded'
headersobject | null
onErrorfunction
(error) => void
auth
이름타입필수기본값
apiKeystring
'x-API-key' header
bearerstring
'Authorization Bearer' header

Returns

string | null | object

Examples

javascript
console.log(await fetchData('https://github.com'), { get: true });
python
print(fetchData('https://github.com'), { 'get': True })

Released under the MIT License