---
title: "HTTP požiadavky"
project: Total.js V4
version: 0.0.99
section: reference
language: sk
license: MIT
url: https://totaljs.es/docs/sk/total-js-v4/0-0-99/reference/http-requests/
updated: 2026-11-16T15:57:56.811Z
---

# HTTP požiadavky

> RESTBuilder, REQUEST() a DOWNLOAD(): klienti pre iné HTTP služby.

Globálne funkcie a metódy `RESTBuilder`, ktoré odosielajú požiadavky iným službám. Návod [HTTP požiadavky](../../guides/http-requests/) vysvetľuje na príkladoch, ako ich používať; táto stránka je presná referencia.

## API

### REQUEST(options, [callback])

Type: `method`

Odošle HTTP požiadavku a vráti surovú odpoveď.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | Voľby požiadavky (pozri tabuľku). |
| `callback` | `Function` | no |  | `function(err, response)`. Bez neho metóda vracia promise. |

Returns: `Promise|undefined` Promise s odpoveďou, keď neodovzdáš callback.

Nízkoúrovňový HTTP klient frameworku; používajú ho `RESTBuilder` a `DOWNLOAD()`. Bez callbacku vráti promise s odpoveďou.

**Voľby**

| Voľba | Typ | Popis |
| --- | --- | --- |
| `url` | String | Adresa (`http://` alebo `https://`) |
| `method` | String | HTTP metóda, predvolene `GET` |
| `headers` | Object | Hlavičky požiadavky |
| `query` | Object/String | Pripojí sa k URL ako query string |
| `body` | String/Buffer | Telo; s `files` objekt polí formulára |
| `type` | String | Nastaví `Content-Type`: `json`, `urlencoded`, `xml`, `plain`, `html` alebo `raw` (`application/octet-stream`) |
| `files` | Array | Súbory na nahratie ako `multipart/form-data`: `{ name, filename, buffer }`, `{ name, filename, path }` alebo `{ name, filename, url }` |
| `cookies` | Object | Cookies na odoslanie, ako `{ name: value }` |
| `cook` | Boolean | Zbiera cookies z odpovedí (aj z presmerovaní) do `options.cookies` |
| `timeout` | Number | Milisekundy; predvolene `CONF.default_restbuilder_timeout` (`10000`); `0` ho vypne |
| `encoding` | String | Kódovanie textovej odpovede, predvolene `utf8`; `binary` vráti `Buffer` |
| `custom` | Boolean | Vráti `response.stream` namiesto čítania tela |
| `noredirect` | Boolean | Nenasleduje presmerovania `301` a `302` |
| `keepalive` | Boolean | Opätovne používa spojenia (nie s proxy) |
| `proxy` | String | Proxy server, napríklad `http://user:pass@host:8080` |
| `resolve` / `dnscache` | Boolean | Preloží názov hostiteľa raz a adresu uloží do cache |
| `xhr` | Boolean | Pridá `X-Requested-With: XMLHttpRequest` |
| `unixsocket` | Object | `{ socket, path }` odošle požiadavku na Unix socket namiesto `url` |
| `key`, `cert`, `dhparam` | Buffer | Klientsky TLS certifikát |
| `insecure` | Boolean | Prijíma neplatné TLS certifikáty |
| `compress` | String | Skomprimuje telo pomocou `gzip` alebo `deflate` a nastaví `Content-Encoding` |
| `encrypt` | String | Zašifruje telo týmto kľúčom (hlavička `X-Encryption`) a dešifruje textovú odpoveď |
| `onprogress` | Function | `fn(percentage)` počas sťahovania tela |
| `ondata` | Function | `fn(chunk, percentage)` pre každý chunk |
| `callback` | Function | To isté ako argument `callback` |

**Odpoveď**: `status`, `headers`, `body`, `host`, `origin` (preložené adresy, s `resolve`) a `cookies`; s `custom: true` je namiesto `body` `stream`. Chyby: `Invalid hostname`, text stavu `408` po timeoute, `Too many redirects` po štyroch presmerovaniach alebo sieťová chyba (vtedy je `status` `0`).

Example: GET s callbackom
```js
REQUEST({ url: 'https://api.example.com/status/', timeout: 5000 }, function(err, response) {
	if (err) {
		console.log(err);
		return;
	}
	console.log(response.status, response.body);
});
```

Example: POST JSON s promise
```js
var response = await REQUEST({
	url: 'https://api.example.com/items/',
	method: 'POST',
	type: 'json',
	body: JSON.stringify({ name: 'Item' })
});
var data = response.body.parseJSON();
```

Example: Čítanie streamu
```js
REQUEST({ url: 'https://www.example.com/big.csv', custom: true }, function(err, response) {
	if (!err)
		response.stream.pipe(Fs.createWriteStream(PATH.temp('big.csv')));
});
```

### DOWNLOAD(url, filename, [callback], [timeout])

Type: `method`

Stiahne URL do súboru.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String|Object` | yes |  | Absolútna alebo relatívna URL, alebo `{ socket, path }`. |
| `filename` | `String` | yes |  | Cieľový súbor. |
| `callback` | `Function` | no |  | `function(err, response)`. |
| `timeout` | `Number` | no | `CONF.default_restbuilder_timeout` | Milisekundy. |

Returns: `Promise|undefined` Promise s odpoveďou, keď neodovzdáš callback.

Odošle požiadavku `GET` a zapíše telo odpovede do `filename`. Callback sa spustí po zapísaní súboru. Bez callbacku metóda vracia promise.

Relatívna URL (napríklad `/sitemap.xml`) sa vyžiada z tvojej vlastnej aplikácie; kým aplikácia nie je načítaná, volanie čaká. Objekt `{ socket, path }` namiesto URL odošle požiadavku na Unix socket.

Odpoveď obsahuje `status`, `headers` a `filename`. Stavový kód sa nekontroluje: súbor sa zapíše aj pri `404`.

Example: Uloženie obrázka
```js
DOWNLOAD('https://www.example.com/logo.png', PATH.public('img/logo.png'), function(err, response) {
	if (err || response.status !== 200)
		console.log('Download failed');
});
```

Example: S await
```js
var response = await DOWNLOAD('https://www.example.com/data.json', PATH.databases('data.json'), null, 30000);
```

### RESTBuilder.GET(url, [data])

Type: `method`

Vytvorí builder pre požiadavku `GET`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Adresa. |
| `data` | `Object` | no |  | Argumenty query stringu. |

Returns: `RESTBuilder` Nový builder.

`data` sa stane query stringom URL. Požiadavku odošleš pomocou `callback()`, `exec()`, `promise()` alebo `stream()`.

Example: Načítanie zoznamu
```js
RESTBuilder.GET('https://api.example.com/products/', { page: 2, limit: 50 }).callback(function(err, response) {
	// GET https://api.example.com/products/?page=2&limit=50
	console.log(response);
});
```

### RESTBuilder.POST(url, [data])

Type: `method`

Vytvorí builder pre požiadavku `POST`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Adresa. |
| `data` | `Object` | no |  | Telo, odoslané ako JSON. |

Returns: `RESTBuilder` Nový builder.

Dáta sa odošlú ako JSON. Zavolaj `urlencoded()` alebo `xml()`, ak chceš odoslať iný formát.

Example: Požiadavka POST
```js
RESTBuilder.POST('https://api.example.com/products/', { name: 'Bike', price: 150 }).callback(function(err, response, output) {
	console.log(output.status, response);
});
```

### RESTBuilder.PUT(url, [data])

Type: `method`

Vytvorí builder pre požiadavku `PUT`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Adresa. |
| `data` | `Object` | no |  | Telo, odoslané ako JSON. |

Returns: `RESTBuilder` Nový builder.

Dáta sa odošlú ako JSON.

Example: Požiadavka PUT
```js
RESTBuilder.PUT('https://api.example.com/products/P01/', { name: 'Bike', price: 150 }).callback(function(err, response, output) {
	console.log(output.status, response);
});
```

### RESTBuilder.PATCH(url, [data])

Type: `method`

Vytvorí builder pre požiadavku `PATCH`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Adresa. |
| `data` | `Object` | no |  | Telo, odoslané ako JSON. |

Returns: `RESTBuilder` Nový builder.

Dáta sa odošlú ako JSON.

Example: Požiadavka PATCH
```js
RESTBuilder.PATCH('https://api.example.com/products/P01/', { name: 'Bike', price: 150 }).callback(function(err, response, output) {
	console.log(output.status, response);
});
```

### RESTBuilder.DELETE(url, [data])

Type: `method`

Vytvorí builder pre požiadavku `DELETE`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Adresa. |
| `data` | `Object` | no |  | Telo, odoslané ako JSON. |

Returns: `RESTBuilder` Nový builder.

Dáta, ak nejaké sú, sa odošlú ako JSON.

Example: Požiadavka DELETE
```js
RESTBuilder.DELETE('https://api.example.com/products/P01/', { name: 'Bike', price: 150 }).callback(function(err, response, output) {
	console.log(output.status, response);
});
```

### RESTBuilder.HEAD(url)

Type: `method`

Vytvorí builder pre požiadavku `HEAD`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Adresa. |

Returns: `RESTBuilder` Nový builder.

Odpoveď prečítaš v callbacku z `output.status` a `output.headers`.

Example: Existuje súbor?
```js
RESTBuilder.HEAD('https://cdn.example.com/files/report.pdf').callback(function(err, response, output) {
	console.log(output.status === 200, output.headers['content-length']);
});
```

### RESTBuilder.API(url, name, [data])

Type: `method`

Zavolá operáciu API endpointu Total.js.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Adresa API endpointu. |
| `name` | `String` | yes |  | Názov operácie s jej parametrami (napríklad `products_read/P01`). |
| `data` | `Object` | no |  | Dáta operácie. |

Returns: `RESTBuilder` Nový builder.

Odošle `POST` s telom JSON `{ schema: name, data: data }`, čo je formát, ktorý čítajú API routy Total.js.

Example: Volanie operácie
```js
RESTBuilder.API('https://app.example.com/admin/', 'products_save', { name: 'Bike' }).callback(function(err, response) {
	console.log(err, response);
});
```

### RESTBuilder.make(fn)

Type: `method`

Vytvorí prázdny builder a odovzdá ho funkcii.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(builder)`. |

Returns: `RESTBuilder` Nový builder.

`fn(builder)` nastaví URL, metódu a všetko ostatné. `RESTBuilder.url(url)` vytvorí builder iba s URL a metóda inštancie `builder.make(fn)` zavolá `fn` s builderom ako `this` aj ako argumentom.

Example: Zostavenie požiadavky
```js
RESTBuilder.make(function(builder) {
	builder.url('https://api.example.com/orders/');
	builder.post({ product: 'P01' });
	builder.header('X-Token', CONF.token);
	builder.callback(console.log);
});
```

### RESTBuilder.upgrade(fn)

Type: `method`

Zmení každý builder pred odoslaním jeho požiadavky.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(builder)`. |

Returns: `undefined`

`fn(builder)` sa spustí v `exec()` pre každý builder aplikácie, po príprave tela a pred odoslaním požiadavky. Zaregistruj ju raz, v súbore definícií.

Example: Pridanie hlavičky do všetkých požiadaviek
```js
// definitions/restbuilder.js
RESTBuilder.upgrade(function(builder) {
	builder.header('X-App', 'eshop');
});
```

### RESTBuilder.addTransform(name, fn, [isDefault])

Type: `method`

Zaregistruje funkciu, ktorá mení spracované odpovede.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov transformácie. |
| `fn` | `Function` | yes |  | `function(value, output)`, ktorá vráti novú hodnotu. |
| `isDefault` | `Boolean` | no | `false` | Použiť ju pre všetky buildery. |

Returns: `undefined`

`fn(value, output)` sa spustí s `this` ako builderom a vráti novú hodnotu. Builder ju použije po `setTransform(name)`; s `isDefault` ju použije každý nový builder. `RESTBuilder.setDefaultTransform(name)` zmení predvolenú transformáciu a `RESTBuilder.setDefaultTransform()` ju odstráni. Transformácia sa nespustí, keď je odpoveď pole chýb Total.js, ktoré sa stane `err`.

Example: Rozbalenie dátovej obálky
```js
RESTBuilder.addTransform('envelope', function(value, output) {
	return value && value.data ? value.data : value;
});

RESTBuilder.GET('https://api.example.com/users/').setTransform('envelope').callback(function(err, users) {
	console.log(users);
});
```

### builder.url([url])

Type: `method`

Prečíta alebo nastaví URL.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | no |  | Adresa. |

Returns: `RESTBuilder|String` Builder alebo URL pri volaní bez argumentu.

Bez argumentu vráti aktuálnu URL.

Example: Nastavenie URL
```js
var builder = RESTBuilder.GET('https://api.example.com/');
builder.url('https://api.example.com/v2/products/');
```

### builder.method(method, [data])

Type: `method`

Nastaví HTTP metódu.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `method` | `String` | yes |  | Napríklad `POST` alebo `put`. |
| `data` | `Object` | no |  | Telo, odoslané ako JSON. |

Returns: `RESTBuilder` Builder, na reťazenie.

Názvy malými písmenami sa prevedú na veľké. `data` sa odošlú ako JSON.

Example: Výber metódy
```js
RESTBuilder.make(function(builder) {
	builder.url('https://api.example.com/items/P01/');
	builder.method('PUT', { price: 20 });
	builder.callback(console.log);
});
```

### builder.get([data])

Type: `method`

Prepne na `GET` a nastaví query string.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `data` | `Object` | no |  | Argumenty query stringu. |

Returns: `RESTBuilder` Builder, na reťazenie.

`data` nahradí query. Alias `builder.GET()`.

Example: Query
```js
RESTBuilder.make(function(builder) {
	builder.url('https://api.example.com/search/');
	builder.get({ q: 'bike' });
	builder.callback(console.log);
});
```

### builder.post([data])

Type: `method`

Prepne na `POST` a nastaví telo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `data` | `Object|String` | no |  | Telo. |

Returns: `RESTBuilder` Builder, na reťazenie.

Telo si ponechá typ nastavený predtým (`urlencoded()`, `xml()`), inak je to JSON. Alias `builder.POST()`.

Example: POST
```js
RESTBuilder.make(function(builder) {
	builder.url('https://api.example.com/orders/');
	builder.post({ product: 'P01', count: 1 });
	builder.callback(console.log);
});
```

### builder.put([data])

Type: `method`

Prepne na `PUT` a nastaví telo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `data` | `Object|String` | no |  | Telo. |

Returns: `RESTBuilder` Builder, na reťazenie.

`builder.patch([data])` a `builder.delete([data])` fungujú rovnako s `PATCH` a `DELETE`; `builder.head()` prepne na `HEAD`. Každá má alias veľkými písmenami (`PUT()`, `PATCH()`, `DELETE()`, `HEAD()`).

Example: PUT a DELETE
```js
RESTBuilder.make(function(builder) {
	builder.url('https://api.example.com/items/P01/');
	builder.put({ price: 20 });
	builder.callback(console.log);
});

RESTBuilder.make(function(builder) {
	builder.url('https://api.example.com/items/P02/');
	builder.delete();
	builder.callback(console.log);
});
```

### builder.header(name, value)

Type: `method`

Nastaví hlavičku požiadavky.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov hlavičky. |
| `value` | `String` | yes |  | Hodnota. |

Returns: `RESTBuilder` Builder, na reťazenie.

Pre CSRF token môžeš použiť aj `builder.csrf(value)` (`X-Csrf-Token`) a `builder.origin(value)` pre `Origin`.

Example: Hlavička s tokenom
```js
RESTBuilder.GET('https://api.example.com/me/').header('X-Token', CONF.token).callback(console.log);
```

### builder.type(value)

Type: `method`

Nastaví hlavičku `Content-Type`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | Typ obsahu. |

Returns: `RESTBuilder` Builder, na reťazenie.

Prepíše typ, ktorý nastavia `json()`, `xml()` a ostatné metódy pre telo.

Example: Vlastný typ
```js
RESTBuilder.POST('https://api.example.com/events/').raw(JSON.stringify(data), 'json').type('application/vnd.api+json').callback(console.log);
```

### builder.accept(type)

Type: `method`

Nastaví hlavičku `Accept`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `type` | `String` | yes |  | Prípona alebo typ obsahu. |

Returns: `RESTBuilder` Builder, na reťazenie.

Krátka hodnota je prípona (`json`, `xml`) a prevedie sa na svoj typ obsahu; dlhšia hodnota sa použije tak, ako je.

Example: Žiadosť o XML
```js
RESTBuilder.GET('https://api.example.com/feed/').accept('xml').callback(console.log);
```

### builder.auth(user, [password])

Type: `method`

Nastaví hlavičku `Authorization`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `user` | `String` | yes |  | Meno používateľa alebo celá hodnota hlavičky. |
| `password` | `String` | no |  | Heslo. |

Returns: `RESTBuilder` Builder, na reťazenie.

S heslom vytvorí Basic autentifikáciu. Bez neho je `user` celá hodnota, napríklad `Bearer TOKEN`.

Example: Basic a Bearer
```js
RESTBuilder.GET('https://api.example.com/').auth('admin', 'secret').callback(console.log);
RESTBuilder.GET('https://api.example.com/').auth('Bearer ' + CONF.token).callback(console.log);
```

### builder.cookie(name, value)

Type: `method`

Pridá cookie do požiadavky.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov. |
| `value` | `String` | yes |  | Hodnota. |

Returns: `RESTBuilder` Builder, na reťazenie.

`builder.cookies(obj)` nahradí všetky cookies objektom `{ name: value }`.

Example: Odoslanie session cookie
```js
RESTBuilder.GET('https://www.example.com/account/').cookie('session', token).callback(console.log);
```

### builder.cook([enable])

Type: `method`

Zbiera cookies z odpovedí.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `enable` | `Boolean` | no | `true` | `false` to vypne. |

Returns: `RESTBuilder` Builder, na reťazenie.

Cookies nastavené serverom, aj počas presmerovaní, sa uložia do objektu z `cookies()` a vracia ich `output.cookie(name)`. Odovzdaj ten istý objekt ďalšiemu builderu, aby si zachoval session.

Example: Zachovanie session
```js
var jar = {};
RESTBuilder.POST('https://www.example.com/login/', { name: 'user', password: 'pass' }).cookies(jar).cook().callback(function(err, response, output) {
	RESTBuilder.GET('https://www.example.com/account/').cookies(jar).callback(console.log);
});
```

### builder.json([data])

Type: `method`

Odošle telo ako JSON.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `data` | `Object|String` | no |  | Telo. |

Returns: `RESTBuilder` Builder, na reťazenie.

Z `GET` sa stane `POST`.

Example: Telo JSON
```js
RESTBuilder.make(function(builder) {
	builder.url('https://api.example.com/items/');
	builder.json({ name: 'Item' });
	builder.callback(console.log);
});
```

### builder.urlencoded([data])

Type: `method`

Odošle telo ako `application/x-www-form-urlencoded`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `data` | `Object|String` | no |  | Telo. |

Returns: `RESTBuilder` Builder, na reťazenie.

Z `GET` sa stane `POST`. Objekt sa zakóduje ako formulár.

Example: Telo formulára
```js
RESTBuilder.POST('https://www.example.com/contact/').urlencoded({ email: 'info@example.com', message: 'Hello' }).callback(console.log);
```

### builder.xml([data], [replace])

Type: `method`

Odošle telo ako XML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `data` | `String` | no |  | XML. |
| `replace` | `Boolean` | no | `false` | Odovzdá sa do `parseXML()` pre odpoveď. |

Returns: `RESTBuilder` Builder, na reťazenie.

Nastaví `Content-Type: text/xml`; z `GET` sa stane `POST`. XML odošli ako reťazec. Odpoveď XML sa spracuje pomocou `String.parseXML()`; `replace` je jeho argument.

Example: Volanie v štýle SOAP
```js
RESTBuilder.make(function(builder) {
	builder.url('https://service.example.com/rates/');
	builder.xml('<request><currency>EUR</currency></request>');
	builder.callback(console.log);
});
```

### builder.plain([value])

Type: `method`

Odošle textové telo a vráti odpoveď ako text.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | no |  | Telo. |

Returns: `RESTBuilder` Builder, na reťazenie.

Nastaví `Content-Type: text/plain`. Odpoveď sa nespracúva: `response` je surové telo.

Example: Čítanie textu
```js
RESTBuilder.GET('https://www.example.com/robots.txt').plain().callback(function(err, text) {
	console.log(text);
});
```

### builder.raw(value, type)

Type: `method`

Nastaví telo a jeho typ.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object|String|Buffer` | yes |  | Telo. |
| `type` | `String` | yes |  | Typ tela. |

Returns: `RESTBuilder` Builder, na reťazenie.

`type` je `json`, `urlencoded`, `xml`, `plain`, `html` alebo `raw` (`application/octet-stream`). Objekt sa serializuje pri odoslaní požiadavky, okrem typu `raw`: tam odošli reťazec alebo `Buffer`.

Example: Binárne telo
```js
RESTBuilder.POST('https://api.example.com/blob/').raw(buffer, 'raw').callback(console.log);
```

### builder.set(name, [value])

Type: `method`

Nastaví jedno pole tela.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String|Object` | yes |  | Pole alebo objekt polí. |
| `value` | `Object` | no |  | Hodnota. |

Returns: `RESTBuilder` Builder, na reťazenie.

`name` môže byť objekt s viacerými poľami. `builder.rem(name)` odstráni pole. S `file()` sa polia odošlú ako polia formulára.

Example: Polia
```js
RESTBuilder.POST('https://api.example.com/items/').set('name', 'Bike').set({ price: 150, stock: 3 }).callback(console.log);
```

### builder.file(name, filename, [source])

Type: `method`

Pridá súbor na nahratie.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov poľa formulára. |
| `filename` | `String` | yes |  | Názov súboru. |
| `source` | `Buffer|String` | no |  | Obsah, cesta alebo URL. |

Returns: `RESTBuilder` Builder, na reťazenie.

Požiadavka sa stane `multipart/form-data` a z `GET` sa stane `POST`. `source` je `Buffer`, cesta alebo URL; URL sa stiahne a prepošle. Bez `source` sa `filename` číta ako cesta a odošle sa iba jeho názov. Typ obsahu sa určí z prípony `filename`.

Example: Nahratie dvoch súborov
```js
RESTBuilder.POST('https://api.example.com/upload/')
	.file('photo', 'photo.jpg', PATH.public('img/photo.jpg'))
	.file('data', 'data.json', Buffer.from(JSON.stringify(data)))
	.callback(console.log);
```

### builder.timeout(ms)

Type: `method`

Nastaví timeout.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `ms` | `Number` | yes | `10000` | Milisekundy. |

Returns: `RESTBuilder` Builder, na reťazenie.

Predvolená hodnota buildera je `10000`. Po timeoute je `err` text stavu `408`.

Example: Krátky timeout
```js
RESTBuilder.GET('https://api.example.com/health/').timeout(2000).callback(console.log);
```

### builder.keepalive()

Type: `method`

Opätovne používa spojenia.

Returns: `RESTBuilder` Builder, na reťazenie.

Požiadavka používa zdieľaného keep-alive agenta. S proxy nemá žiadny účinok.

Example: Veľa volaní na jeden hostiteľ
```js
RESTBuilder.GET('https://api.example.com/items/').keepalive().callback(console.log);
```

### builder.proxy(url)

Type: `method`

Odošle požiadavku cez proxy server.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Napríklad `http://user:password@proxy.example.com:8080`. |

Returns: `RESTBuilder` Builder, na reťazenie.

URL môže obsahovať prihlasovacie údaje. Pre všetky požiadavky nastav `CONF.default_proxy`.

Example: Proxy
```js
RESTBuilder.GET('https://api.example.com/').proxy('http://proxy.example.com:3128').callback(console.log);
```

### builder.cert(key, cert, [dhparam])

Type: `method`

Nastaví klientsky TLS certifikát.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `Buffer` | yes |  | Súkromný kľúč. |
| `cert` | `Buffer` | yes |  | Certifikát. |
| `dhparam` | `Buffer` | no |  | Parametre Diffie-Hellman. |

Returns: `RESTBuilder` Builder, na reťazenie.

Použi ho pre služby, ktoré vyžadujú klientske certifikáty. `builder.insecure()` prijíma neplatné serverové certifikáty (iba na testovanie).

Example: Klientsky certifikát
```js
RESTBuilder.GET('https://bank.example.com/api/')
	.cert(Fs.readFileSync(PATH.root('client.key')), Fs.readFileSync(PATH.root('client.crt')))
	.callback(console.log);
```

### builder.nodnscache()

Type: `method`

Vypne DNS cache pre túto požiadavku.

Returns: `RESTBuilder` Builder, na reťazenie.

Builder preloží názov hostiteľa raz a adresu si ponechá; toto ho preloží ako zvyčajne. Lokálne adresy sa nikdy neukladajú do cache.

Example: Čerstvé DNS
```js
RESTBuilder.GET('https://api.example.com/').nodnscache().callback(console.log);
```

### builder.redirect(enable)

Type: `method`

Zapne alebo vypne nasledovanie presmerovaní.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `enable` | `Boolean` | yes |  | Nasledovať presmerovania. |

Returns: `RESTBuilder` Builder, na reťazenie.

`redirect(false)` vráti samotnú odpoveď `301` alebo `302`. Presmerovania sa predvolene nasledujú.

Example: Čítanie location
```js
RESTBuilder.GET('https://short.example.com/abc').redirect(false).callback(function(err, response, output) {
	console.log(output.headers.location);
});
```

### builder.unixsocket(socket, path)

Type: `method`

Odošle požiadavku na Unix socket.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `socket` | `String` | yes |  | Cesta k súboru socketu. |
| `path` | `String` | yes |  | Cesta požiadavky. |

Returns: `RESTBuilder` Builder, na reťazenie.

URL nastavená predtým sa stane cestou.

Example: Lokálna služba
```js
RESTBuilder.make(function(builder) {
	builder.unixsocket('/var/run/app.sock', '/api/status/');
	builder.callback(console.log);
});
```

### builder.xhr()

Type: `method`

Pridá `X-Requested-With: XMLHttpRequest`.

Returns: `RESTBuilder` Builder, na reťazenie.

Example: XHR
```js
RESTBuilder.GET('https://www.example.com/api/').xhr().callback(console.log);
```

### builder.referrer(value)

Type: `method`

Nastaví hlavičku `Referer`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | URL. |

Returns: `RESTBuilder` Builder, na reťazenie.

Alias `builder.referer()`.

Example: Referer
```js
RESTBuilder.GET('https://www.example.com/page/').referrer('https://www.example.com/').callback(console.log);
```

### builder.encrypt([key])

Type: `method`

Zašifruje telo a dešifruje odpoveď.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `key` | `String` | no | `DEF.secret_encryption` | Kľúč. |

Returns: `RESTBuilder` Builder, na reťazenie.

Použije šifrovanie frameworku s `key` alebo `DEF.secret_encryption` a pridá `X-Encryption`. Obe strany musia používať rovnaký kľúč.

Example: Šifrované volanie
```js
RESTBuilder.POST('https://node2.example.com/sync/', data).encrypt(CONF.sync_key).callback(console.log);
```

### builder.cache(expire)

Type: `method`

Uloží úspešnú odpoveď do cache.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `expire` | `String` | yes |  | Napríklad `5 minutes`. |

Returns: `RESTBuilder` Builder, na reťazenie.

Odpoveď so stavom `200` sa uloží do `F.cache` na dobu `expire`; kľúč sa vytvorí z URL a tela. `builder.nocache()` vynechá cache pre jedno volanie.

Example: Cache kurzov
```js
RESTBuilder.GET('https://api.example.com/rates/').cache('1 hour').callback(console.log);
```

### builder.schema(name)

Type: `method`

Overí odpoveď schémou.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov schémy. |

Returns: `RESTBuilder` Builder, na reťazenie.

Callback dostane model vytvorený schémou. Chyby schémy a stavový kód `400` alebo vyšší sa vrátia ako `err`. Metóda vyhodí výnimku, keď schéma neexistuje.

Example: Overenie odpovede
```js
NEWSCHEMA('Rate', function(schema) {
	schema.define('currency', 'String(3)', true);
	schema.define('value', Number, true);
});

RESTBuilder.GET('https://api.example.com/rate/EUR/').schema('Rate').callback(function(err, rate) {
	console.log(err, rate);
});
```

### builder.map(map)

Type: `method`

Ponechá a skonvertuje vybrané polia odpovede.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `map` | `String` | yes |  | Napríklad `id:number,name:string,dtcreated:date:created`. |

Returns: `RESTBuilder` Builder, na reťazenie.

`map` uvádza dvojice `field:type` oddelené čiarkami. Tretia časť pole premenuje: `source:type:target`. Funguje pre objekt aj pre pole objektov.

Example: Mapovanie zoznamu
```js
RESTBuilder.GET('https://api.example.com/users/').map('id:number,name:string').callback(function(err, users) {
	console.log(users);
});
```

### builder.convert(schema)

Type: `method`

Skonvertuje polia odpovede.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `schema` | `String` | yes |  | Napríklad `price:number,date:date`. |

Returns: `RESTBuilder` Builder, na reťazenie.

Odpoveď prejde cez `CONVERT(value, schema)`. Nespúšťa sa spolu so `schema()`.

Example: Konverzia typov
```js
RESTBuilder.GET('https://api.example.com/order/1/').convert('total:number,created:date').callback(console.log);
```

### builder.strict()

Type: `method`

Považuje stavové kódy `400` a vyššie za chyby.

Returns: `RESTBuilder` Builder, na reťazenie.

Potom je `err` stavový kód. Bez toho musíš `output.status` kontrolovať sám.

Example: Chyba pri 404
```js
RESTBuilder.GET('https://api.example.com/items/X/').strict().callback(function(err, response) {
	if (err === 404)
		console.log('Not found');
});
```

### builder.noparse()

Type: `method`

Vráti surové telo bez spracovania.

Returns: `RESTBuilder` Builder, na reťazenie.

`response` je telo ako text (alebo `Buffer` pri binárnych typoch).

Example: Surový JSON
```js
RESTBuilder.GET('https://api.example.com/export/').noparse().callback(function(err, body) {
	Fs.writeFile(PATH.temp('export.json'), body, NOOP);
});
```

### builder.setTransform(name)

Type: `method`

Použije zaregistrovanú transformáciu.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov transformácie. |

Returns: `RESTBuilder` Builder, na reťazenie.

Pozri `RESTBuilder.addTransform()`.

Example: Použitie transformácie
```js
RESTBuilder.GET('https://api.example.com/users/').setTransform('envelope').callback(console.log);
```

### builder.progress(fn)

Type: `method`

Hlási priebeh sťahovania.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function` | yes |  | `function(percentage)`. |

Returns: `RESTBuilder` Builder, na reťazenie.

`fn(percentage)` sa spustí pre každý chunk. Potrebuje hlavičku `Content-Length` v odpovedi; bez nej je percento `0`.

Example: Priebeh
```js
RESTBuilder.GET('https://www.example.com/large.json').progress(function(percentage) {
	console.log(percentage.floor(1) + '%');
}).callback(console.log);
```

### builder.debug()

Type: `method`

Zaloguje požiadavku a odpoveď.

Returns: `RESTBuilder` Builder, na reťazenie.

Vypíše do konzoly stav, metódu, URL, chybu a surové telo.

Example: Ladenie
```js
RESTBuilder.GET('https://api.example.com/').debug().callback(console.log);
```

### builder.callback([fn])

Type: `method`

Pošle požiadavku.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `fn` | `Function|Object` | no |  | `function(err, response, output)` alebo `$`. |

Returns: `RESTBuilder|Promise` Builder, alebo promise bez funkcie.

S funkciou sa spustí `fn(err, response, output)` s výsledkom. Bez funkcie, alebo s `$` operácie schémy, metóda vráti promise: s `$` ide chyba do `$.invalid()`.

Example: Callback
```js
RESTBuilder.GET('https://api.example.com/items/').callback(function(err, response, output) {
	console.log(err, response, output.status);
});
```

Example: Promise
```js
var items = await RESTBuilder.GET('https://api.example.com/items/').callback();
```

### builder.exec([callback])

Type: `method`

Odošle požiadavku okamžite.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | `function(err, response, output)`. |

Returns: `RESTBuilder` Builder, na reťazenie.

`callback(err, response, output)` je rovnaký ako v `callback()`; `exec()` odošle okamžite, `callback()` v ďalšom ticku.

Example: Exec
```js
RESTBuilder.GET('https://api.example.com/ping/').exec(function(err, response) {
	console.log(response);
});
```

### builder.promise([$])

Type: `method`

Odošle požiadavku a vráti promise.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `$` | `Object` | no |  | Options operácie. |

Returns: `Promise` Promise so spracovanou odpoveďou.

Promise sa splní so spracovanou odpoveďou. S `$` (options operácie schémy) sa chyba odovzdá do `$.invalid()` a promise sa neukončí; bez neho sa promise zamietne.

Example: V operácii
```js
schema.action('sync', {
	name: 'Sync',
	action: async function($) {
		var items = await RESTBuilder.GET('https://api.example.com/items/').promise($);
		$.callback(items);
	}
});
```

### builder.stream(callback)

Type: `method`

Odošle požiadavku a vráti odpoveď ako stream.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | yes |  | `function(err, response)`. |

Returns: `RESTBuilder` Builder, na reťazenie.

`callback(err, response)` dostane `status`, `headers`, `origin` a `stream` (prichádzajúcu správu). Telo sa nečíta ani nespracúva.

Example: Presmerovanie do súboru
```js
RESTBuilder.GET('https://www.example.com/export.csv').stream(function(err, response) {
	if (!err)
		response.stream.pipe(Fs.createWriteStream(PATH.temp('export.csv')));
});
```

### output.cookie(name)

Type: `method`

Prečíta cookie z odpovede.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Názov cookie. |

Returns: `String` Hodnota.

`output` je tretí argument callbacku. Metóda prečíta hlavičky `Set-Cookie` alebo cookies zozbierané pomocou `cook()` a vráti dekódovanú hodnotu alebo prázdny reťazec.

Example: Čítanie cookie
```js
RESTBuilder.POST('https://www.example.com/login/', { name: 'user', password: 'pass' }).callback(function(err, response, output) {
	var session = output.cookie('session');
});
```
