Skip to content

HTTP API

Toss POS의 HTTP 클라이언트를 사용하여 외부 API를 호출할 수 있는 기능을 제공합니다. 다양한 HTTP 메서드를 지원하며, 요청 헤더와 응답 처리를 위한 타입을 제공합니다.

Types

PluginHttpResponse

HTTP 요청의 응답을 나타내는 객체입니다.

ts
{
    body: string;           // 응답 본문
    headers: [string, string][];  // 응답 헤더
    code: number;          // HTTP 상태 코드
}
필드타입설명예시
bodystring응답 본문{"key": "value"}
headers[string, string][]응답 헤더[['Content-Type', 'application/json']]
codenumberHTTP 상태 코드200

Methods

get

GET 요청을 보냅니다.

ts
import { posPluginSdk } from '@tossplace/pos-plugin-sdk';

const response = await posPluginSdk.http.get('https://api.example.com', [
    ['Content-Type', 'application/json'],
    ['Connection', 'keep-alive'],
]);

post

POST 요청을 보냅니다.

ts
import { posPluginSdk } from '@tossplace/pos-plugin-sdk';

const data = { key: 'value' };
const response = await posPluginSdk.http.post('https://api.example.com', data, [
    ['Content-Type', 'application/json'],
    ['Connection', 'keep-alive'],
]);

put

PUT 요청을 보냅니다.

ts
import { posPluginSdk } from '@tossplace/pos-plugin-sdk';

const data = { key: 'value' };
const response = await posPluginSdk.http.put('https://api.example.com', data, [
    ['Content-Type', 'application/json'],
    ['Connection', 'keep-alive'],
]);

patch

PATCH 요청을 보냅니다.

ts
import { posPluginSdk } from '@tossplace/pos-plugin-sdk';

const data = { key: 'value' };
const response = await posPluginSdk.http.patch('https://api.example.com', data, [
    ['Content-Type', 'application/json'],
    ['Connection', 'keep-alive'],
]);

delete

DELETE 요청을 보냅니다.

ts
import { posPluginSdk } from '@tossplace/pos-plugin-sdk';

const response = await posPluginSdk.http.delete('https://api.example.com', [
    ['Content-Type', 'application/json'],
    ['Connection', 'keep-alive'],
]);

HEAD 요청을 보냅니다.

ts
import { posPluginSdk } from '@tossplace/pos-plugin-sdk';

const response = await posPluginSdk.http.head('https://api.example.com', [
    ['Content-Type', 'application/json'],
    ['Connection', 'keep-alive'],
]);

options

OPTIONS 요청을 보냅니다.

ts
import { posPluginSdk } from '@tossplace/pos-plugin-sdk';

const response = await posPluginSdk.http.options('https://api.example.com', [
    ['Content-Type', 'application/json'],
    ['Connection', 'keep-alive'],
]);

사용 예시

API 호출 및 응답 처리

ts
import { posPluginSdk } from '@tossplace/pos-plugin-sdk';

async function fetchData() {
    try {
        const response = await posPluginSdk.http.get('https://api.example.com/data', [
            ['Content-Type', 'application/json'],
            ['Authorization', 'Bearer token'],
        ]);

        if (response.code === 200) {
            const data = JSON.parse(response.body);
            console.log('데이터:', data);
        } else {
            console.error('API 호출 실패:', response.code);
        }
    } catch (error) {
        console.error('에러 발생:', error);
    }
}