11cb0ef41Sopenharmony_ci# Class: ProxyAgent
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciExtends: `undici.Dispatcher`
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciA Proxy Agent class that implements the Agent API. It allows the connection through proxy in a simple way.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci## `new ProxyAgent([options])`
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciArguments:
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci* **options** `ProxyAgentOptions` (required) - It extends the `Agent` options.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciReturns: `ProxyAgent`
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci### Parameter: `ProxyAgentOptions`
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciExtends: [`AgentOptions`](Agent.md#parameter-agentoptions)
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci* **uri** `string` (required) - It can be passed either by a string or a object containing `uri` as string.
201cb0ef41Sopenharmony_ci* **token** `string` (optional) - It can be passed by a string of token for authentication.
211cb0ef41Sopenharmony_ci* **auth** `string` (**deprecated**) - Use token.
221cb0ef41Sopenharmony_ci* **clientFactory** `(origin: URL, opts: Object) => Dispatcher` (optional) - Default: `(origin, opts) => new Pool(origin, opts)`
231cb0ef41Sopenharmony_ci* **requestTls** `BuildOptions` (optional) - Options object passed when creating the underlying socket via the connector builder for the request. See [TLS](https://nodejs.org/api/tls.html#tlsconnectoptions-callback).
241cb0ef41Sopenharmony_ci* **proxyTls** `BuildOptions` (optional) - Options object passed when creating the underlying socket via the connector builder for the proxy server. See [TLS](https://nodejs.org/api/tls.html#tlsconnectoptions-callback).
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciExamples:
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci```js
291cb0ef41Sopenharmony_ciimport { ProxyAgent } from 'undici'
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciconst proxyAgent = new ProxyAgent('my.proxy.server')
321cb0ef41Sopenharmony_ci// or
331cb0ef41Sopenharmony_ciconst proxyAgent = new ProxyAgent({ uri: 'my.proxy.server' })
341cb0ef41Sopenharmony_ci```
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci#### Example - Basic ProxyAgent instantiation
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciThis will instantiate the ProxyAgent. It will not do anything until registered as the agent to use with requests.
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci```js
411cb0ef41Sopenharmony_ciimport { ProxyAgent } from 'undici'
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciconst proxyAgent = new ProxyAgent('my.proxy.server')
441cb0ef41Sopenharmony_ci```
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci#### Example - Basic Proxy Request with global agent dispatcher
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci```js
491cb0ef41Sopenharmony_ciimport { setGlobalDispatcher, request, ProxyAgent } from 'undici'
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciconst proxyAgent = new ProxyAgent('my.proxy.server')
521cb0ef41Sopenharmony_cisetGlobalDispatcher(proxyAgent)
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciconst { statusCode, body } = await request('http://localhost:3000/foo')
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_cifor await (const data of body) {
591cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // data foo
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci```
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci#### Example - Basic Proxy Request with local agent dispatcher
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci```js
661cb0ef41Sopenharmony_ciimport { ProxyAgent, request } from 'undici'
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciconst proxyAgent = new ProxyAgent('my.proxy.server')
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciconst {
711cb0ef41Sopenharmony_ci  statusCode,
721cb0ef41Sopenharmony_ci  body
731cb0ef41Sopenharmony_ci} = await request('http://localhost:3000/foo', { dispatcher: proxyAgent })
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciconsole.log('response received', statusCode) // response received 200
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cifor await (const data of body) {
781cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')) // data foo
791cb0ef41Sopenharmony_ci}
801cb0ef41Sopenharmony_ci```
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci#### Example - Basic Proxy Request with authentication
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci```js
851cb0ef41Sopenharmony_ciimport { setGlobalDispatcher, request, ProxyAgent } from 'undici';
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ciconst proxyAgent = new ProxyAgent({
881cb0ef41Sopenharmony_ci  uri: 'my.proxy.server',
891cb0ef41Sopenharmony_ci  // token: 'Bearer xxxx'
901cb0ef41Sopenharmony_ci  token: `Basic ${Buffer.from('username:password').toString('base64')}`
911cb0ef41Sopenharmony_ci});
921cb0ef41Sopenharmony_cisetGlobalDispatcher(proxyAgent);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ciconst { statusCode, body } = await request('http://localhost:3000/foo');
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ciconsole.log('response received', statusCode); // response received 200
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_cifor await (const data of body) {
991cb0ef41Sopenharmony_ci  console.log('data', data.toString('utf8')); // data foo
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci```
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci### `ProxyAgent.close()`
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ciCloses the proxy agent and waits for registered pools and clients to also close before resolving.
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciReturns: `Promise<void>`
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci#### Example - clean up after tests are complete
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci```js
1121cb0ef41Sopenharmony_ciimport { ProxyAgent, setGlobalDispatcher } from 'undici'
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ciconst proxyAgent = new ProxyAgent('my.proxy.server')
1151cb0ef41Sopenharmony_cisetGlobalDispatcher(proxyAgent)
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ciawait proxyAgent.close()
1181cb0ef41Sopenharmony_ci```
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci### `ProxyAgent.dispatch(options, handlers)`
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ciImplements [`Agent.dispatch(options, handlers)`](Agent.md#parameter-agentdispatchoptions).
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci### `ProxyAgent.request(options[, callback])`
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ciSee [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback).
127