11cb0ef41Sopenharmony_ci# Connecting through a proxy
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciConnecting through a proxy is possible by:
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci- Using [AgentProxy](../api/ProxyAgent.md).
61cb0ef41Sopenharmony_ci- Configuring `Client` or `Pool` constructor.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciThe proxy url should be passed to the `Client` or `Pool` constructor, while the upstream server url
91cb0ef41Sopenharmony_cishould be added to every request call in the `path`.
101cb0ef41Sopenharmony_ciFor instance, if you need to send a request to the `/hello` route of your upstream server,
111cb0ef41Sopenharmony_cithe `path` should be `path: 'http://upstream.server:port/hello?foo=bar'`.
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciIf you proxy requires basic authentication, you can send it via the `proxy-authorization` header.
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci### Connect without authentication
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci```js
181cb0ef41Sopenharmony_ciimport { Client } from 'undici'
191cb0ef41Sopenharmony_ciimport { createServer } from 'http'
201cb0ef41Sopenharmony_ciimport proxy from 'proxy'
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciconst server = await buildServer()
231cb0ef41Sopenharmony_ciconst proxyServer = await buildProxy()
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst serverUrl = `http://localhost:${server.address().port}`
261cb0ef41Sopenharmony_ciconst proxyUrl = `http://localhost:${proxyServer.address().port}`
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciserver.on('request', (req, res) => {
291cb0ef41Sopenharmony_ci  console.log(req.url) // '/hello?foo=bar'
301cb0ef41Sopenharmony_ci  res.setHeader('content-type', 'application/json')
311cb0ef41Sopenharmony_ci  res.end(JSON.stringify({ hello: 'world' }))
321cb0ef41Sopenharmony_ci})
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst client = new Client(proxyUrl)
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst response = await client.request({
371cb0ef41Sopenharmony_ci  method: 'GET',
381cb0ef41Sopenharmony_ci  path: serverUrl + '/hello?foo=bar'
391cb0ef41Sopenharmony_ci})
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciresponse.body.setEncoding('utf8')
421cb0ef41Sopenharmony_cilet data = ''
431cb0ef41Sopenharmony_cifor await (const chunk of response.body) {
441cb0ef41Sopenharmony_ci  data += chunk
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ciconsole.log(response.statusCode) // 200
471cb0ef41Sopenharmony_ciconsole.log(JSON.parse(data)) // { hello: 'world' }
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciserver.close()
501cb0ef41Sopenharmony_ciproxyServer.close()
511cb0ef41Sopenharmony_ciclient.close()
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_cifunction buildServer () {
541cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
551cb0ef41Sopenharmony_ci    const server = createServer()
561cb0ef41Sopenharmony_ci    server.listen(0, () => resolve(server))
571cb0ef41Sopenharmony_ci  })
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_cifunction buildProxy () {
611cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
621cb0ef41Sopenharmony_ci    const server = proxy(createServer())
631cb0ef41Sopenharmony_ci    server.listen(0, () => resolve(server))
641cb0ef41Sopenharmony_ci  })
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci```
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci### Connect with authentication
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci```js
711cb0ef41Sopenharmony_ciimport { Client } from 'undici'
721cb0ef41Sopenharmony_ciimport { createServer } from 'http'
731cb0ef41Sopenharmony_ciimport proxy from 'proxy'
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciconst server = await buildServer()
761cb0ef41Sopenharmony_ciconst proxyServer = await buildProxy()
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciconst serverUrl = `http://localhost:${server.address().port}`
791cb0ef41Sopenharmony_ciconst proxyUrl = `http://localhost:${proxyServer.address().port}`
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ciproxyServer.authenticate = function (req, fn) {
821cb0ef41Sopenharmony_ci  fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
831cb0ef41Sopenharmony_ci}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ciserver.on('request', (req, res) => {
861cb0ef41Sopenharmony_ci  console.log(req.url) // '/hello?foo=bar'
871cb0ef41Sopenharmony_ci  res.setHeader('content-type', 'application/json')
881cb0ef41Sopenharmony_ci  res.end(JSON.stringify({ hello: 'world' }))
891cb0ef41Sopenharmony_ci})
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ciconst client = new Client(proxyUrl)
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ciconst response = await client.request({
941cb0ef41Sopenharmony_ci  method: 'GET',
951cb0ef41Sopenharmony_ci  path: serverUrl + '/hello?foo=bar',
961cb0ef41Sopenharmony_ci  headers: {
971cb0ef41Sopenharmony_ci    'proxy-authorization': `Basic ${Buffer.from('user:pass').toString('base64')}`
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci})
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciresponse.body.setEncoding('utf8')
1021cb0ef41Sopenharmony_cilet data = ''
1031cb0ef41Sopenharmony_cifor await (const chunk of response.body) {
1041cb0ef41Sopenharmony_ci  data += chunk
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ciconsole.log(response.statusCode) // 200
1071cb0ef41Sopenharmony_ciconsole.log(JSON.parse(data)) // { hello: 'world' }
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciserver.close()
1101cb0ef41Sopenharmony_ciproxyServer.close()
1111cb0ef41Sopenharmony_ciclient.close()
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_cifunction buildServer () {
1141cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
1151cb0ef41Sopenharmony_ci    const server = createServer()
1161cb0ef41Sopenharmony_ci    server.listen(0, () => resolve(server))
1171cb0ef41Sopenharmony_ci  })
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_cifunction buildProxy () {
1211cb0ef41Sopenharmony_ci  return new Promise((resolve, reject) => {
1221cb0ef41Sopenharmony_ci    const server = proxy(createServer())
1231cb0ef41Sopenharmony_ci    server.listen(0, () => resolve(server))
1241cb0ef41Sopenharmony_ci  })
1251cb0ef41Sopenharmony_ci}
1261cb0ef41Sopenharmony_ci```
1271cb0ef41Sopenharmony_ci
128